Score:0

cron/php how to run command as a non root user

in flag

I need to run command (only one command) as a non root user from cron.

Have tried two versions and both fails;

Cron:

* * * * * php script.php

script.php:

exec("whoami"); // returns 'root'
// version 1
exec("runuser -u www-data -- ls"); // error -> sh: 1: runuser: not found
// version 2
exec("su www-data -c 'ls'"); // error -> This account is currently not available.
Score:1
jp flag

Don't use root crontab. Use crontab for www-data user:

crontab -e -u www-data

or use a system crontab in /etc/cron.d/ and specify user www-data.

* * * * * www-data php script.php
holden321 avatar
in flag
I need run as root. I need run only one command as non root, because it is execute program that not working under root. ls-command is just for example.
jp flag
Your question states the following "I need to run command (only one command) as a non root user from cron".
in flag
Run the script as non-root and execute the commands that require root via sudo. Everything else is a potential security risk.
Phill  W. avatar
cn flag
@holden321: You can have a crontab for each and every user on your machine and each crontab will operate independently of every other. The purpose of cron is to start processes periodically. There are no "dependencies" or "sequencing" between individual crontab entries - they are simply invoked when the timing criteria tell it to do so - the there is /no reason/ to start processes under other accounts.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.