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
I assume you only want to be added to
crontab
ONCE. Therefore one must remove the script from thecrontab
first:Hello and welcome to Ask Ubuntu!
You can write to
crontab
viastdin
: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:
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 anat
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, anat
job. You may need to install theat
command viasudo 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 intoat
. Some examples:In your case:
This will run
test-script.sh
the next time it is 11pm.