crontab command failing

wallis

New Member
Joined
May 15, 2024
Messages
16
Reaction score
3
Credits
214
I have a command in crontab as follows : * /5 0-23 * * * /home/pi/refresh.sh

refresh.sh contains the following two lines and is executed every 5 minutes:
line1 contains: /usr/bin/sudo pkill -F /home/pi/metarpid.pid (purpose is to stop the currently executing metar.py )
line2 contains: /usr/bin/sudo python3 /home/pi/metar.py & echo $! > /home/pi/metarpid.pid (purpose is to re-start metar.py and place its new pid in the file metarpid.pid)

Every 5 minutes, the crontab job runs the commands in refresh.sh to kill the running process of metar.py and the second line is used to start metar.py back running.

Every 5 minutes I get the following 2 lines of output from cron output from syslog using: tail -f /var/log/syslog | grep CRON
Jun 11 15:55:01 metar4 CRON[2138]: (pi) CMD (/home/pi/refresh.sh)
Jun 11 15:55:01 metar4 [2144] (CRON) info (No MTA installed, disregarding output)

metar4 is the raspberry pi hostname.

When the CRON job runs, the program metar.py does not start and the file metarpid.pid is not created and placed in /home/pi as expected.
However, if I type the contents of line 2 in in a terminal window, the metar.py program starts running and metarpid.pid is created in /home/pi as expected.

I do not understand why the crontab command is getting the MTA error which I would think is why metar.py does not start and its pid file not placed in /home/pi.
I am not using email either, which is what I think MTA is referring.
 


I have a command in crontab as follows : * /5 0-23 * * * /home/pi/refresh.sh

refresh.sh contains the following two lines and is executed every 5 minutes:
line1 contains: /usr/bin/sudo pkill -F /home/pi/metarpid.pid (purpose is to stop the currently executing metar.py )
line2 contains: /usr/bin/sudo python3 /home/pi/metar.py & echo $! > /home/pi/metarpid.pid (purpose is to re-start metar.py and place its new pid in the file metarpid.pid)

Every 5 minutes, the crontab job runs the commands in refresh.sh to kill the running process of metar.py and the second line is used to start metar.py back running.

Every 5 minutes I get the following 2 lines of output from cron output from syslog using: tail -f /var/log/syslog | grep CRON
Jun 11 15:55:01 metar4 CRON[2138]: (pi) CMD (/home/pi/refresh.sh)
Jun 11 15:55:01 metar4 [2144] (CRON) info (No MTA installed, disregarding output)

metar4 is the raspberry pi hostname.

When the CRON job runs, the program metar.py does not start and the file metarpid.pid is not created and placed in /home/pi as expected.
However, if I type the contents of line 2 in in a terminal window, the metar.py program starts running and metarpid.pid is created in /home/pi as expected.

I do not understand why the crontab command is getting the MTA error which I would think is why metar.py does not start and its pid file not placed in /home/pi.
I am not using email either, which is what I think MTA is referring.
Below are some observations which may be useful.

In the crontab command there shouldn't be a space between the first asterisk and /5:
* /5 0-23 * * * /home/pi/refresh.sh
Correct format would be */5 0-23 * * * /home/pi/refresh.sh
This is possibly a transcription typo, but I mention it because it's there.

In the refresh.sh:
/usr/bin/sudo pkill -F /home/pi/metarpid.pid
/usr/bin/sudo python3 /home/pi/metar.py & echo $! > /home/pi/metarpid.pid

presumably sudo doesn't need to ask for a password to run those commands. That would usually need to be configured in the /etc/sudoers file or in a file in the /etc/sudoers.d/ directory.

One could consider running the script as root if it needs root privileges which would obviate the need for sudo.

The command, echo $!, usually yields the pid of a process that has been set to run in the background. But there is no indication of a background process using the usual "&" after a relevant command. May or may not be an issue in your case.

By default with cron the output of a command or a script (if any is produced), will be emailed to your local email account. To stop receiving email output from crontab you need to append:
/dev/null 2>&1
For example:
0 3 * * * /dir/shell.sh >/dev/null 2>&1

The log on the MTA is only info and not an error and thus of no consequence in relation to the failure of the script to run under cron.

One could amend the shell script to include full paths, for example:
/usr/bin/pkill and /usr/bin/python3, and see it that helps since there is no path configured in the script.

If all else fails, then the simplest solution is to write a systemd unit file which will run the script and write a systemd timer file to trigger it. In any case, if your system is running systemd, it is systemd that starts the cron service, though the cron daemon still controls the cron jobs, but it means that the systemd cron.service should be enabled.
 
Last edited:


Follow Linux.org

Members online


Top