I have a simple bash script running the following command:
service mysql status | mail -s "MySQL service" myname@example.com
I have added it to the crontab of the root user:
sudo crontab -e
Using the following command:
*/1 * * * * bash /home/myname/cron/mysql.sh
And the resulting email contains this:
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.```
```Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.```
```Server version 5.5.37-0ubuntu0.14.04.1```
```Protocol version 10```
```Connection Localhost via UNIX socket```
```UNIX socket /var/run/mysqld/mysqld.sock```
```Uptime: 3 hours 25 min 22 sec```
These complicated statistics are are not expected value from the 'service mysql status' command.
When I run the batch file manually as root:
``` sudo bash mysql.sh ```
The resulting email contains the expected result:
``` mysql start/running, process 11928 ```
So why is the 'service mysql status' command returning an unexpected value when run from a batch file inside the root cron job?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I found my answer here:
http://stackoverflow.com/a/13521933
Basically the cron jobs operate in a limited environment, so in 'start [service]', the start command is not found!
In my case, the ‘service’ command is part of the ‘Upstart’ daemon of Ubuntu, and by default the cron process doesn’t have the path to it.
Adding the following to my bash script fixed the issue:
#!/bin/bash
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.