0

I Have a script that needs to be run for each user at a specific time.

Is there a way of updating (crontab -e or similar) each of the users' crontabs as root ?

Alternatively, what is the accepted way of having roots' crontab assume each user and run the script?

Redhat specific answers appreciated.

  • As Christopher mentioned, I suspect that you have problem like 'how will I run a script for every user in the system?' and you are stuck with one of your solutions. I would like to point out that you could use root's crontab with su command (check out su - username -c whoami run from root). Also ask yourself how are you going to maintain user crons if they will temper with it (most probably users should have permissions to do so)? Problem is to gather usernames. You could check passwd file with ids above 1000 (please, verify if it fits your system). – Kalavan Nov 30 '16 at 15:37
2

A clean solution is to use

( crontab -l -u user ; cat addition ) | crontab -u user -

and interating by every user. You can use the output of cd /home ; ls on common configurations.

| improve this answer | |
1

You can write a script that add the line that you want to add to the crontab to all files in the directory that crontab files saved:

/var/spool/cron/

script like:

for file in /var/spool/cron/*
do
    # do something on $file like
    echo "$CRONTABCOMMADN" >> $file
done
| improve this answer | |
  • Note: not all users have a crontab, so you need to create for the missing user (and possibly exclude for the users not in group users. – Giacomo Catenazzi Nov 30 '16 at 13:52
  • that's true but he said he want to update so i assumed that all users are already have ones – Wissam Roujoulah Nov 30 '16 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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