Ubuntu – How to write into cronjob from a shell script

bashcommand linecronksh

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

Best Answer

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 -