Wednesday, July 3, 2013

Using pipes for creating crontab entries

We can add crontab entry using the following command

echo "0 0 * * * CmdToBeExecuted" | crontab

But, this will remove all existing crontab entries and add this new entry alone. I realized this when none of my other cron based things doesn't work.

Here is the correct way to add a crontab entry.

crontab -l > crontab_entries.txt
echo "Cron_Entry_Text" >> crontab_entries.txt
cat crontab_entries.txt | crontab

Check by listing the crontab entries

crontab -l

Now, if your crontab already has your script and you want to update it, filter it out and then do an update.

crontab -l | grep -v "ScriptName" > crontab_entries.txt
echo "Cron_Entry_Text" >> crontab_entries.txt
cat crontab_entries.txt | crontab

This will filter out your existing crontab entry, so you don't add a duplicate entry. But, be sure that your grep matches only the correct ones that you want to update.

Here is the quick way to remove a crontab entry.

crontab -l | grep -v "ScriptTobeRemoved" | crontab

I have made this post for recording a learning. Hope, this will be helpful for someone someday.

No comments:

Post a Comment