How to kill a root's screen via php?


I’m thinking to write a php script to kill a particular screen when meeting criteria and the screen is require root access. I notice when you call the php script it will use apache as user. How do i set it as root? Below code does not work for me. Please help.

$whom = shell_exec(whoami);
$output = shell_exec('sudo /usr/bin/screen -dr testScreen -X quit');

Thank you,

Add apache as the co-owner of the screen? That way you don’t have to sudo

That would sound nothing more then a security nightmare…

emmm … any workaround?

I use php because need to query some condition from database.
Is it possible to run query in shell script? If possible then I can set this script to root’s cron. Then should have root access to kill the screen.

Please advise. Thanks.

Run the screen as Apache is one solution, give Apache access to the screen is another (a security disaster waiting to happen).

A better solution, might be to signal to the screen it needs to die, I’m assuming you are running a PHP script in the screen? If so, maybe get it to check a status file / flag somewhere and if it finds it, clean up and stop.

Thanks for reply. I’ve found using shell script is the best solution for me. Just schedule it as root’s cron then it will have the privilege to kill screen at my prefer time and prefer screen. :slight_smile:


result=$(mysql -u $user_name $db_name -p$password -sN -e "SELECT COUNT(id) AS num FROM tblQue WHERE status='P'")
if [ "$result" -lt 100 ];
    echo "Close screen"
    /usr/bin/screen -dr scnProcessA -X quit

You could have called your PHP script from cron as well i.e.

php /path/to/your/script.php


I’ve tried that. But apache has no access to kill root’s screen. unless you grant root access to apache but that would be a security risk as mentioned by logic_earth. :slight_smile:


Erm… Apache would have nothing to do with it unless you were running it from the apache users cron. PHP doesn’t have to be called via the server, you can set a root cron job up i.e.

*/5 * * * * php /path/to/your/script.php

which will execute your PHP script from the command line as the root user, assuming you’ve got CLI PHP installed (which is most likely).


Hi Karl,

Thanks. It really works.

*/5 * * * * php /path/to/your/script.php
shell_exec('/usr/bin/screen -dr testScreen -X quit');

Sorry. It was my mistake. Because I use curl in my cron. Thats why shell_exec not work in php.

00 07 * * * /usr/bin/curl