Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When a cron job runs with what privilege does it execute?

I'm not sure about that. Is it with the same privileges of the user that added it via crontab -e?

share|improve this question
    
"Is it with the same privileges of the user that added it via crontab -e?" Yes. Or technically the user whose crontab it's in. –  Kevin Jul 4 '13 at 23:43
add comment

2 Answers 2

You can specify a user in the system crontab entries like so:

# For details see man 4 crontabs

# Example of job definition:
.---------------- minute (0 - 59)
|  .------------- hour (0 - 23)
|  |  .---------- day of month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
|  |  |  |  |
*  *  *  *  * user-name  command to be executed

The 6th argument can be a username. Additionally you can put scripts in the /etc/cron.d directory. The scripts take the same form as the crontab entries described above, for example:

# /etc/cron.d/clamav-update
## Adjust this line...
MAILTO=root

## It is ok to execute it as root; freshclam drops privileges and becomes
## user 'clamav' as soon as possible
0  */3 * * * root /usr/share/clamav/freshclam-sleep

You can put scripts in these directories, but they're meant to be run as root:

  • cron.daily
  • cron.hourly
  • cron.weekly
  • cron.monthly

Finally you can create user based crontab entries by running this command as a given user:

$ crontab -e

These entries are stored in files with the same name as the user in this directory, /var/spool/cron/:

$ sudo ls -l /var/spool/cron/
-rw------- 1 saml root 0 Jun  6 06:43 saml
share|improve this answer
add comment

Yes, however, jobs that are added manually to the systems crontab (edit /etc/crontab) will be run with absolute permissions (ie: run as root) unless you specify another user.

share|improve this answer
    
How would you specify a non-root user? –  Jim Jul 4 '13 at 21:48
    
Just use crontab -e normally as a normal user and it will be run as that user. :) –  haneefmubarak Jul 4 '13 at 22:53
add comment

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.