24

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?

1
  • "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
    Commented Jul 4, 2013 at 23:43

2 Answers 2

25

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
7

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.

3
  • How would you specify a non-root user?
    – Jim
    Commented Jul 4, 2013 at 21:48
  • 1
    Just use crontab -e normally as a normal user and it will be run as that user. :) Commented Jul 4, 2013 at 22:53
  • Or just specify the 'user-name' cronjob parameter Commented Feb 10, 2015 at 11:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .