通过Bash/Shell在Crontab中启用/禁用任务

use*_*988 10 linux bash shell crontab

有没有办法使用Bash/Shell启用和禁用Crontab任务?

因此,当用户启动Server 1时,它将启用Server 1 Crontab行,依此类推.当用户停止服务器1时,服务器1 Crontab行被禁用(#).这可能吗?怎么样?

提前致谢

*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
Run Code Online (Sandbox Code Playgroud)

Bar*_*mar 14

SERVERNUM=$1
Run Code Online (Sandbox Code Playgroud)

启用:

crontab -l | sed "/^#.*Server $SERVERNUM check/s/^#//" | crontab -
Run Code Online (Sandbox Code Playgroud)

要禁用:

crontab -l | sed "/^[^#].*Server $SERVERNUM check/s/^/#/" | crontab -
Run Code Online (Sandbox Code Playgroud)

成绩单:

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
Run Code Online (Sandbox Code Playgroud)