Centos – How to use crontab to run a script as nobody

centoscroncrontabscript

This is on a CentOS machine. I'm trying to run a script as user nobody (or as a user with minimal permissions) at a certain time every day. Here is nobody:

[root@CentOS % ~] grep "^nobody" /etc/passwd  
nobody:x:99:99:Nobody:/:/sbin/nologin  

here's what I've tried in root's crontab:

setting the Environment variable SUDO_USER=nobody
15 17 * * * sudo -u nobody /bin/bash /usr/local/bin/bashscript.sh
15 17 * * * su -c /usr/local/bin/bashscript.sh nobody

I'd like to keep the crontab entry in root's crontab if at all possible. I'd also prefer not fooling with user nobody's account, as I don't want to break anything else that might rely on those settings. I'm not adverse to creating another non-privileged account and giving them a real shell if that's the sticking point.

I'll also admit to being a bit perplexed. I would assume this would be an everyday issue, except my brown belt in google-fu isn't helping much.

Best Answer

I'm guessing you are posting the contents of crontab -e or crontab -l?

This is the crontab file beloning to user "root", and that file does not support specifying a user to run the command as (as it's generally a file used for scheduling personal jobs).
Look at /etc/crontab instead which is the system-wide crontab and has an additional field: the user field. Try adding a line like this to /etc/crontab:

15 17 * * * nobody /usr/local/bin/bashscript.sh
Related Question