Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

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?

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.