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 -
- Schedule script execution using Task Scheduler of Windows, which
will login to Server to execute the script. Check this link - 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
Difference Between Linux Loadable and Built-In Modules
Bash: Difference Between "Export K=1" VS. "K=1"
Selecting the Right Linux I/O Scheduler for a Host Equipped with Nvme Ssd
Change Directory and Execute File in One Command
How to Extract Characters Between the Delimiters Using Sed
Using Rsync Include and Exclude Options to Include Directory and File by Pattern
Managing a User Password for Linux in Puppet
One Command to Create a Directory and File Inside It Linux Command
Simulate a Faulty Block Device with Read Errors
Can Ebpf Modify the Return Value or Parameters of a Syscall
Sort Across Multiple Files in Linux
Differencebetween .So and .A Files
Using Find - Deleting All Files/Directories (In Linux ) Except Any One
What's the Difference Between Tempfile and Mktemp
How to Get a List of All Valid Ip Addresses in a Local Network
Can You Prevent a Command from Going into the Bash Shell Command History