In multi-user systems with no restrictions on who can and who cannot schedule cron jobs, things could go out of hand pretty quickly. So it is advisable to check time to time who is running what to ensure that not many users are running cpu intensive operations or they are not stepping on each others’ toes and end up wasting CPU.
To find who has cron jobs scheduled on a system there is no built in command. There is a workaround though. But you have to be superuser or root for this. /var/spool/cron directory contains one file for each user (the file is named as the user) that has any cron job scheduled. So a simple ls /var/spool/cron will provide the list of users that have cron jobs scheduled in the system. Continue reading “cron : List users who have cron jobs scheduled”
If you have auto installing packages there could be times when a cron job needs to be added. So the script has to be able to create new cron entries or delete old ones. One solution is to create temporary files in between to hold the other unaffected cron entries that are currently installed, add the new entry and then install this file using crontab. Creating of temporary files should be avoided in between as there are risks. So here is an elegant solution which uses piping in the output of multiple commands.
To remove already existing cron job (rdate for user unixite in example below) use a syntax like
crontab -l -u unixite | grep -v rdate | crontab -u unixite -
This pipe chain lists the existing crontab entries, removes any containing the string rdate, then reloads the resulting data by piping it back to crontab of user unixite. “-” is for reading from the stream or terminal (see Note below). No useless temporary file, no security risk.
To add new crontab entries Continue reading “bash : Automate add/modify/delete of cron jobs from a script”