What are the runtime permissions of a cron job

cronpermissions

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?

Best Answer

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
Related Question