I want to centralize a lot of crons to /etc/crontab. Now every user gots it own crons in /var/spool/cron, which is not maintaineble for me. I want to do this automated via a script, but the problem is that the cron syntax differs. In /etc/crontab an user must be stated. How can I get this task done efficiently?
Asked
Active
Viewed 174 times
2 Answers
2
You might start with something like:
while read user;
do crontab -l -u $user >> usersTabs;
done < <(awk -F: '{print $1}' /etc/passwd )
It really depends, do you want them to run as the same user they were before?

Kyle Brandt
- 83,619
- 74
- 305
- 448
0
If my google is correct, the difference in the format is:
* syntax:
* user crontab:
* minutes hours doms months dows cmd\n
* system crontab (/etc/crontab):
* minutes hours doms months dows USERNAME cmd\n
Assuming you have the output from users crontabs in a folder, you could do something like:
find . -type f -print0 | xargs -0 -e perl -pe "s/((\w+\s+){5})(.*)/\1USERNAME \3/;"
I don't know if this crontab output makes sense for you, so double check that. It should be possible to make this script even simpler with better perl or using a different tool like awk or sed, but I can't remember the syntax right now.