Jack Pettersson
Jack Pettersson

Reputation: 1666

Creating a cronjob through script

Is there any way to create a cronjob in an automated way (script)?

The only way i know how to create a cronjob is through contab -e which creates a temporary conf file that is, once modified and exited, merged with the actual conf file in /var/spool/cron/crontabs (this file can't be modified directly).

In particular what i'm trying to do is package a script that needs to be run every minute. When the user installs the package, the cronjob needs to be added and there in lies my problem.

So: I need a script to create a script that runs my script, not that confusing right? :P

Upvotes: 0

Views: 75

Answers (1)

Barmar
Barmar

Reputation: 780673

You can use

crontab filename

to install filename as the user's crontab. So your script can do something like:

crontab -l > crontab.txt # Get current crontab
echo "0 12 * * * /path/to/script" >>crontab.txt #Add a new entry
crontab crontab.txt # install new crontab

Upvotes: 1

Related Questions