Click here to Skip to main content
15,314,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
Bash
#!/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.

What I have 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
Posted
Updated 4-Dec-17 10:42am

1 solution

It can't find service on the path, which appears to be /usr/bin:/bin from the email. I don't know what distro you are using, but my Ubuntu 16.04LTS has it at /usr/sbin/service
Use which service to find it and adjust your script accordingly.
   
Comments
Dev O'Connor 5-Dec-17 4:27am
   
+1 @Peter_in_2780 - spot on advice - worked a treat
Peter_in_2780 5-Dec-17 5:02am
   
You're welcome! cron jobs don't get to run all your nice startup scripts :p Bitten me a few times.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900