Enable/Disable Tasks in Crontab by Bash/Shell

Enable/Disable tasks in Crontab by Bash/Shell

SERVERNUM=$1

To enable:

crontab -l | sed "/^#.*Server $SERVERNUM check/s/^#//" | crontab -

To disable:

crontab -l | sed "/^[^#].*Server $SERVERNUM check/s/^/#/" | crontab -

Transcript:

barmar@dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar@dev$ crontab -l | sed '/^[^#].*Server 1 check/s/^/#/' | crontab -
barmar@dev$ crontab -l
#*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar@dev$ crontab -l | sed '/^#.*Server 1 check/s/^#//' | crontab -
barmar@dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check

How to quickly disable a single job in crontab

The quickest way would be to edit the crontab file (which can be done by typing crontab -e) and simply comment the job you want disabled. Comment lines in crontab start with a #.

0 0 1 * * this_job_i_want.sh

# uncomment below to enable
# 0 0 2 * * this_job_i_dont_want.sh

How to enable and disable cronjobs using shell script

You have to escape '/' characters in your sed command...

sed "/^[^#].*bash \/home\/scripts\/mywork.sh/s/^/#/"

How to schedule the execution of a unix-script without using cron?

Possible Options to Schedule a script -

  1. Schedule script execution using Task Scheduler of Windows, which
    will login to Server to execute the script. Check this link
  2. Expect command of unix which can allows to login to Another Server
    and execute the script. Check this link

How to create a cron job using Bash automatically without the interactive editor?

You can add to the crontab as follows:

#write out current crontab
crontab -l > mycron
#echo new cron into cron file
echo "00 09 * * 1-5 echo hello" >> mycron
#install new cron file
crontab mycron
rm mycron

Cron line explaination

* * * * * "command to be executed"
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Source nixCraft.

Remove @restart cronjob using bash

You can pipe to this sed to delete that line:

sed '\~@reboot /opt/nzbget/nzbget -D~d'


Related Topics



Leave a reply



Submit