0

I would like to automate cronjob (meaning schedule a job to be done from a shell script). So i have script that does this

#!/bin/ksh
timestamp=$(date +%H%M%S)
if [ "$timestamp" -eq 230000 ]; then
 write to cronjob to execute the same script @ 11:00 PM
fi

so the output should be like below, is this possible?

00 23 2 10 2 /home/test/run_cron.sh
4
  • You know that your script will only do something when run at exactly 23:00:00, right? And (only) then it will add a cronjob to run every day at 23:00:00. Next day, it will add another such job. Same for 3rd and 4th and every following day. What's the purpose of this?
    – PerlDuck
    Oct 2, 2018 at 11:36
  • That's correct. Made the right edits. i need it run for that night and not any other night.
    – ady6831983
    Oct 2, 2018 at 18:12
  • Seems you are looking for an at job, not a cron job. at jobs are run just once at some time in the future. Might that better suit your needs?
    – PerlDuck
    Oct 2, 2018 at 18:23
  • How do you manage to run the script at exactly 23:00:00? Else the if won't trigger...
    – PerlDuck
    Oct 2, 2018 at 18:51

3 Answers 3

2

Hello and welcome to Ask Ubuntu!

You can write to crontab via stdin:

echo  '0 23 * * * /home/test/run_cron.sh' | crontab -

The downside is that this clears all the previous crontab entries.

If you must keep already present entries (and not start from scratch), something like this should work:

echo "$(crontab -l ; echo  '0 23 * * * /home/test/run_cron.sh')" | crontab -
2

I assume you only want to be added to crontab ONCE. Therefore one must remove the script from the crontab first:

#!/bin/ksh
myname="$0"  

(crontab -l | grep -E -v "$myname";echo " 0 23 \* \* \* $myname") |\
    crontab -
2
  • Crontab does not have the script when run for the first time correct, why should we remove it?
    – ady6831983
    Oct 2, 2018 at 18:15
  • @ady6831983 Because the next day the crontab will already contain the job and you don't want to add it twice.
    – PerlDuck
    Oct 2, 2018 at 18:24
0

From the comments and your updated question I read you want to run your script not every day at 11pm but only once the next day at 11pm. While this is possible with cron I suggest using an at job instead.

When adding a cronjob for this, we would need to calculate the next day's date (to setup something like 0 23 2 10 * … for Oct 2) and then again remove that cronjob the next day. As said: possible but cumbersome. Hence, an at job. You may need to install the at command via sudo apt install at.

The syntax of at is a bit surprising: the command to be run is not given as a parameter but read from stdin, so we need to pipe it into at. Some examples:

echo "ls -l > out1.txt" | at now + 2 minutes
echo "ls -l > out2.txt" | at 23:00
echo "ls -l > out3.txt" | at tomorrow

In your case:

echo "./test-script.sh" | at 23:00

This will run test-script.sh the next time it is 11pm.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .