RUN A PHP SCRIPT AS ANOTHER USER

Run A PHP Script As Another User From The Terminal

If I want to run a php script from user “fred” even though I’m logged in as say user “raspberry” then:

sudo nano InfiniteRunningPHPScript.php

Type the following:

Run-PHP-Script-As-Another-User

Run the php script as User “fred”:

sudo -H -u fred php InfiniteRunningPHPScript.php
Run-PHP-Script-As-Another-User

→ i.e. This script is just a pointless infinite loop. We can use "ps aux" to observe it running for test purposes and then kill the process once finished...

Note : sudo -u fred php InfiniteRunningPHPScript.php works fine, i.e. without the -H.

Now, from another terminal, let’s check to see which PHP jobs are running via:

ps aux|grep ".php" | grep -v grep

Run-PHP-Script-As-Another-User

• So we can see that User “fred” is running the php script called “InfiniteRunningPHPScript.php”. Success!
• Obviously to stop the php script just press Ctrl C on the Terminal.





Linux Examples - Comments