Do I properly understand that it is not possible to run /etc/cron.daily/my
from arbitrary user, it will be run only from root
according to /etc/crontab
?
Can I drop root privilege to specific user in my cron scrip instead?
It is better to place cron job into /etc/cron.d
? Like:
$ cat /etc/cron.d/my
5 0 * * * user test -x /usr/bin/my && /usr/bin/my -cfg /etc/my.cfg
You are right, the jobs in
/etc/cron.daily
(and weekly/monthly, etc.) are always executed as userroot
but you can simply swith the user from within the script and call that very script again as that other user, including all supplied arguments (although there won't be any in acron.daily
job):File
/etc/cron.daily/my
:When the script is started as user
root
it will detect so and re-execute itself viasudo -H -u gavenkoa
, that is: as usergavenkoa
. This requires no special entries in/etc/sudoers
becauseroot
is always allowed to switch to any user.The
exec
replaces the current process with the newsudo …
call and never returns. That's why you don't need anelse
clause.