I'm going to make a bash script that is executed at boot and runs periodically.
I want it user-configurable, so that a user can add a cron job 0 * * * * my_script
by running my_script add 0 * * * *
, list jobs by my_script list
, and remove by my_script remove job_number
where the job number is listed in the output of my_script list
command.
If I could manage crontab files separately, this would be easily achieved. However, It seems crontab is only one file per a user (If not, please let me know). Directly dealing with that crontab file is a bad solution, of course.
So what is the proper way to handle the cron jobs? Or, is there a better way to handle periodically running scripts?
Conditions:
- Any user should be able to run it, whether privileged or not.
- No dependencies.
Additional question:
Since I couldn't find any proper way to manage periodically running scripts, I thought what I might be doing wrong. In the sense of software design, is it not practical to implement the interface to manage the software's scheduled tasks? Should I leave all schedule managements to users?
/var/cron/tabs
(it may be a different folder on your system). This is not to be modified through other means than by thecrontab
command though, so anything you build will have to use that. You could obviously also have a system where you have multiple crontabs per user, but you would need to concatenate these to have them activated by cron. – Kusalananda May 06 '17 at 16:24run-parts "$HOME/cron/hourly"
(on Linux). Things that are more complicated than a simple command should not be run by cron directly but by a script invoked by cron. If a user has hundreds of tasks that needs to be scheduled, put them in a separate script and schedule that (or userun-parts
as I mentioned). There is absolutely no need for a more complicated system than one that allows a user to schedule multiple things at arbitrary intervals, which is what cron does. – Kusalananda May 06 '17 at 16:49run-parts
it as you mentioned. – queuedq May 06 '17 at 17:32