Skip to content
Advertisement

Execute screen command returning `Cannot make directory ‘/var/run/screen’: File exists`

I’ve no idea what is happening. I can run any other shell command through the exec() command and it works fine. It’s clearly something to do with permissions somewhere, but I’m unsure what. I’ve tried this on many different servers and some of them work fine and some don’t. All were running CentOS. Safe mode is always off.

This is the code I’m trying to get to work:

<?php
error_reporting(E_ALL);

if( ini_get('safe_mode') ){
   echo "safe mode is on";
}else{
   echo "safe mode is not on";
}
echo "nn";

echo "Start Server.....n";

//exec("screen -clean", $err, $err1);
//exec("mkdir /var/test/tewst/");

exec("screen -dms test1 mkdir /var/test/test1/", $err, $err1);

print_r($err);
echo "n" . $err1;
echo "nCommands sent...";

?>

All thats returned is this:

safe mode is not on

Start Server.....
Array
(
    [0] => Cannot make directory '/var/run/screen': File exists
)

1
Commands sent...

Can anyone please shed some light on this at all?

Advertisement

Answer

Update: This was caused by SELinux.

Turns out it was some weird file system/user permissions error. After running ls -al /var/run through the exec() command in PHP, it returned this:

total 52
drwxrwxrwx. 23 root   root   4096 Oct 12 15:13 .
drwxr-xr-x. 22 root   root   4096 Oct 10 08:50 ..
d??????????  ? ?      ?         ?            ? ConsoleKit
drwxr-xr-x.  2 root   root   4096 Oct 11 18:13 abrt
-rw-r--r--.  1 root   root      5 Oct 11 18:13 abrtd.pid
-??????????  ? ?      ?         ?            ? acpid.pid
s??????????  ? ?      ?         ?            ? acpid.socket
-??????????  ? ?      ?         ?            ? atd.pid
-??????????  ? ?      ?         ?            ? auditd.pid
-??????????  ? ?      ?         ?            ? autofs-running
p??????????  ? ?      ?         ?            ? autofs.fifo-misc
p??????????  ? ?      ?         ?            ? autofs.fifo-net
-??????????  ? ?      ?         ?            ? autofs.pid
drwxr-xr-x.  2 root   root   4096 Feb 22  2013 certmonger
-??????????  ? ?      ?         ?            ? certmonger.pid
d??????????  ? ?      ?         ?            ? console
-??????????  ? ?      ?         ?            ? cron.reboot
-??????????  ? ?      ?         ?            ? crond.pid
d??????????  ? ?      ?         ?            ? cups
-??????????  ? ?      ?         ?            ? cupsd.pid
drwxr-xr-x.  2 root   root   4096 Oct 11 18:13 dbus
drwxr-xr-x.  2 root   root   4096 Feb 21  2013 faillock
d??????????  ? ?      ?         ?            ? hald
-??????????  ? ?      ?         ?            ? haldaemon.pid
drwx--x---.  2 root   apache 4096 Oct 12 15:26 httpd
d??????????  ? ?      ?         ?            ? lvm
s??????????  ? ?      ?         ?            ? mcelog-client
-??????????  ? ?      ?         ?            ? mcelog.pid
d??????????  ? ?      ?         ?            ? mdadm
-??????????  ? ?      ?         ?            ? messagebus.pid
drwxr-xr-x.  2 mysql  mysql  4096 Oct 11 18:13 mysqld
drwxrwxr-x.  2 root   root   4096 Feb 22  2013 netreport
d??????????  ? ?      ?         ?            ? plymouth
d??????????  ? ?      ?         ?            ? pm-utils
d??????????  ? ?      ?         ?            ? portreserve
-??????????  ? ?      ?         ?            ? rpc.statd.pid
-??????????  ? ?      ?         ?            ? rpcbind.lock
-??????????  ? ?      ?         ?            ? rpcbind.pid
s??????????  ? ?      ?         ?            ? rpcbind.sock
d??????????  ? ?      ?         ?            ? saslauthd
d??????????  ? ?      ?         ?            ? screen
d??????????  ? ?      ?         ?            ? sepermit
drwxr-xr-x.  2 root   root   4096 May 10 05:06 setrans
-??????????  ? ?      ?         ?            ? sm-notify.pid
-??????????  ? ?      ?         ?            ? sshd.pid
-??????????  ? ?      ?         ?            ? syslogd.pid
-??????????  ? ?      ?         ?            ? utmp
drwxr-xr-x.  2 root   root   4096 Feb 21  2013 winbindd

I fixed it by deleting the directory and making the /var/run permissions 0777. This meant that the web server user could create the directory and use it as expected.

rm /var/run/screen -R -f
chmod 0777 /var/run

I still don’t know why this has happened, nor why there are question marks all over the place in the ls command. If anyone sees this and knows, please leave a comment or answer explaining why. It will be a big help, since I know have more errors.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement