Score:0

Script executed from crontab is not running

mk flag

Good day all,

I have a Docker Container with Ubuntu OS and MySQL installed.

I need to restart the MySQL once a day and would normally use cron to do this.

When I run the script via the CLI the scripts runs perfectly within the Docker Container.

Script contents:

#!/bin/bash

echo "Stopping MySQL" 
service mysql stop 
sleep 15s 
echo "Starting MySQL" 
service mysql start
sleep 10s 
echo "MySql Restarted" 
service mysql status 
sleep 10s

Log when running from CLI, using ./mysqlRestart >>/var/log/mysqlRestart 2>&1:

Stopping MySQL

Stopping MySQL database server mysqld ...done. 
Starting MySQL
Starting MySQL database server mysqld su: warning: cannot change directory to /nonexistent: No such file or directory ...done. 
MySql Restarted
/usr/bin/mysqladmin Ver 8.0.30-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu)) Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Server version 8.0.30-0ubuntu0.20.04.2 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 11 sec

Threads: 2 Questions: 8 Slow queries: 0 Opens: 119 Flush tables: 3 Open tables: 38 Queries per second avg: 0.727

crontab -e entry: */5 * * * * bash /usr/local/bin/mysqlRestart >>/var/log/mysqlRestart 2>&1

Log when running from crontab:

Stopping MySQL /usr/local/bin/mysqlRestart: line 4: service: command not found 
Starting MySQL /usr/local/bin/mysqlRestart: line 7: service: command not found 
MySql Restarted /usr/local/bin/mysqlRestart: line 10: service: command not found
Score:1
in flag

The super user account that the cron job is running under does not know where to find service. You will need to use the full path to the command:

/usr/sbin/service mysql restart

You may want o double-check that run time, though, as every five minutes is a bit excessive. If your database needs to be restarted every day, that generally points to:

  • inefficient SQL queries
  • insufficient memory
  • poor I/O performance

If it's at all possible, it may be worthwhile looking at optimising your Docker container settings to reduce the need for this sort of workaround

James Tavendale avatar
mk flag
Hi, Thanks for the feedback I will test and revert
James Tavendale avatar
mk flag
The 5 minute internal was purely for my testing. It will be changed to a longer interval once I have the cronjib working. Thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.