Cronjob under Plesk/Debian/Linux does not work – this is how it works every minute/second

Cronjob under Plesk/Debian/Linux does not work – this is how it works every minute/second

You have used

crontab -e

to add a new cronjob (executed every minute), but the cronjob is not executed?

Here sometimes an error can creep in, if you use the “/home” directory. This directory is also mapped under “/root” depending on the user. Accordingly you should not store your scripts in the “/home” directory, but under another folder and then refer to it in your crontab.

Additionally you should put in the first line of your shell script

#!/bin/bash

in the first line of your shell script.

Also it can be sometimes useful to redirect the output to a logfile, here is an example to execute a shellscript with logfile…

Crontab examples

Every minute:

*/1 * * * /root/test.sh >> /root/test.log 2>&1

Every hour at minute 0:

0 * * * /root/test.sh >> /root/test.log 2>&1

Every day at 0:

0 0 * * * /root/test.sh >> /root/test.log 2>&1

Every month on the first day at 0 o’clock:

0 0 1 * * /root/test.sh >> /root/test.log 2>&1

Every Monday per week at 0 o’clock:

0 0 * * 1 /root/test.sh >> /root/test.log 2>&1

You don’t want a logfile? Then just replace

>> /root/test.log 2>&1

with

> /dev/null 2>&1

Run cronjob every second

It is not intended to run a cronjob every second, so you need a workaround. Such a workaround could be e.g. the creation of 60 equal cronjobs which are all executed with a time delay.

*/1 * * * /root/second/0.sh >> /root/test.log 2>&1
*/1 * * * /root/second/1.sh >> /root/test.log 2>&1
*/1 * * * /root/second/2.sh >> /root/test.log 2>&1
*/1 * * * /root/second/3.sh >> /root/test.log 2>&1
*/1 * * * /root/second/4.sh >> /root/test.log 2>&1
...

In 0.sh you would then insert above:

#!/bin/bash
wait 0
/root/second/run.sh

In 1.sh you would then insert above:

#!/bin/bash
wait 1
/root/second/run.sh

In 2.sh you would then insert above:

#!/bin/bash
wait 2
/root/second/run.sh

Endless loop

An alternative to this would be instead of running a cronjob at the end of the shell script, to call the same shell script again and thus create an infinite loop. Of course, this should not be done as a cronjob, but under a screen session. Because otherwise you would eventually have a large number of endlessly executed scripts. Here is an example to start a screen session

screen -mdS Endless /root/endlessloop.sh

Also it could be advisable to add a small delay with

wait 1

into the shell script.

Leave a Reply

Your email address will not be published.