0

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

That is absolutely brilliant. I had no idea you could specify a "template" crontab file as an argument for the program. And your solution for compatibility with already existing jobs was a bonus. Thanks alot :)
When you're wondering how to do things, don't you look at man pages?
I am also looking for the same but instead of txt file can I use csv?
@subhro The format of a crontab file is pretty specific, it's not a CSV file. You could write a script that converts a CSV file to crontab format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.