0

Evening

i am trying to automate a service status email from a select number of services. when i run the script with sh -x ./myscriptname.sh it works absolutely fine. But when installed as a cron, i get a constant error

service: command not found

The shell script is as follows:

#!/bin/sh service --status-all | grep 'tomcat\|xcoder\|sshd\|xinetd' | mail -s "$(hostname) Daily Service Status Monitor" -r "no-reply@no-company.com" -S smtp="x.x.x.x:25" my.email@address.co.uk

The mail i get is:

Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Mon,  4 Dec 2017 19:32:01 +0000 (WET)
Status: R

/etc/cron.daily/myscriptname.sh: line 2: service: command not found
Null message body; hope that's ok

would appreciate it if anyone could shed some light on this.

i have also tried:

<pre lang="c#">
#!/bin/sh
output=$(service --status-all | grep 'tomcat\|xcoder\|sshd\|xinetd')
echo $output | mail -s "$(hostname) Daily Service Status Monitor"  -r "no-reply@no-company.com" -S smtp="x.x.x.x:25" my.email@address.co.uk

this produces the same response.

  • 1
    Probably the service command is not in cron's default PATH: either use the absolute path e.g. /usr/sbin/service or add the appropriate locations to PATH at the top of the script – steeldriver Dec 4 '17 at 20:00
  • Thanks for your reply, when i try run the command, list it or change directory to it it says it doesnt exist, so i am not sure adding this to the front of the command or adding it to the path file as this is not the location of the service command. do you have any other suggestions? – David OConnor Dec 5 '17 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.