Question

'service mysql status' returns different values

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?


Submit an answer

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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"