0

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.
holden321
  • 107
  • 1
  • 4

1 Answers1

1

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
AlexD
  • 8,747
  • 2
  • 29
  • 38
  • 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. – holden321 Jan 07 '22 at 11:14
  • 1
    Your question states the following "I need to run command (only one command) as a non root user from cron". – AlexD Jan 07 '22 at 11:16
  • 1
    Run the script as non-root and execute the commands that require root via sudo. Everything else is a potential security risk. – Gerald Schneider Jan 07 '22 at 11:52
  • 1
    @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. – Phill W. Jan 07 '22 at 13:50