louxiu
louxiu

Reputation: 2915

write a shell script to add job to cron

I write a shell script below to add a job to cron.

#!/bin/sh
touch date.cron
echo '*/3 * * * * /usr/sbin/ntpdate 192.168.2.3' >date.cron
crontab date.cron
rm date.cron

But I don't want to create the file date.cron. How can I add the job directly without creating the file. Any suggestions?

Upvotes: 2

Views: 8104

Answers (2)

Benoit Thiery
Benoit Thiery

Reputation: 6387

( crontab -l 2>/dev/null | grep -Fv ntpdate ; printf -- "*/3 * * * * /usr/sbin/ntpdate 192.168.2.3" ) | crontab

Upvotes: 9

Luis
Luis

Reputation: 276

Use >> "append" instead of > "output"

#!/bin/bash
Task="'*/3 * * * * /usr/sbin/ntpdate 192.168.2.3'";
$Task >> date.cron;

Greetings.

Upvotes: 1

Related Questions