ChatGPT解决这个技术问题 Extra ChatGPT

How to specify in crontab by what user to run script? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions about general computing hardware and software on Stack Overflow. You can edit the question so it’s on-topic for Stack Overflow. Closed 2 years ago. Improve this question

I have few crontab jobs that run under root, but that gives me some problems. For example all folders created in process of that cron job are under user root and group root. How can i make it to run under user www-data and group www-data so when i run scripts from my website i can manipulate those folders and files?

My server runs on Ubuntu. Current crontab job is:

*/1 * * * * php5 /var/www/web/includes/crontab/queue_process.php >> /var/www/web/includes/crontab/queue.log 2>&1

M
Mike

Instead of creating a crontab to run as the root user, create a crontab for the user that you want to run the script. In your case, crontab -u www-data -e will edit the crontab for the www-data user. Just put your full command in there and remove it from the root user's crontab.


It works the same way when you use crontab -e as a specific user.
But will this crontab be checked at system start-up or only when the user logs in?
cron on *nix systems doesn't require a user to login in order to run the jobs specified in a specific user's crontab.
Will this also set the group id as the OP asked? What if the group one wants is different from the user's primary group?
P
Pamela

EDIT: Note that this method won't work with crontab -e, but only works if you edit /etc/crontab directly. Otherwise, you may get an error like /bin/sh: www-data: command not found

Just before the program name:

*/1 * * * * www-data php5 /var/www/web/includes/crontab/queue_process.php >> /var/www/web/includes/crontab/queue.log 2>&1

That would make it run as apache user witch is www-data right?
Note that this method won't work with crontab -e, but only works if you edit /etc/crontab directly. Read the comment at the top of this file for more information.
The user can only be specified in the system crontab
I have a few jobs on schedule on system crontab. When I do sudo crontab -e, I see the jobs. But I open the file /etc/crontab there are no jobs. Is this weird? Also if I sudo crontab -e and add a job with user specified, will it work?
p
pymkin

Since you're running Ubuntu, your system crontab is located at /etc/crontab.

As the root user (or using sudo), you can simply edit this file and specify the user that should run this command. Here is the format of entries in the system crontab and how you should enter your command:

# m h dom mon dow user  command
*/1 * * * * www-data php5 /var/www/web/includes/crontab/queue_process.php >> /var/www/web/includes/crontab/queue.log 2>&1

Of course the permissions for your php script and your log file should be set so that the www-data user has access to them.


I thought this should be equivalent to crontab -e though; but not.
The Ubuntu docs have recommended not editing /etc/crontab as it can be overwritten by updates. crontab -e will create a user-specific cron file in /var/spool/cron/crontabs.
O
Oliver Moran

Mike's suggestion sounds like the "right way". I came across this thread wanting to specify the user to run vncserver under on reboot and wanted to keep all my cron jobs in one place.

I was getting the following error for the VNC cron:

vncserver: The USER environment variable is not set. E.g.:

In my case, I was able to use sudo to specify who to run the task as.

@reboot sudo -u [someone] vncserver ...

I got this message on the logs: sudo: sorry, you must have a tty to run sudo
@renatoargh You are probably running on RedHat. Take a look at this answer on Unix.SE.
R
Russell E Glaue

You can also try using runuser (as root) to run a command as a different user

*/1 * * * * runuser php5 \
            --command="/var/www/web/includes/crontab/queue_process.php \
                       >> /var/www/web/includes/crontab/queue.log 2>&1"

See also: man runuser


runuser is not included in Ubuntu.
runuser is in the latest version of Ubuntu. There are also alternatives to runuser as discussed in this answer unix.stackexchange.com/questions/169441/ubuntu-runuser-command , e.g. su, and sudo.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now