exec doesn't found a file - php

I'm having problems with the exec php function.
When I do:
ls -al /dev/sdf1
This doesn't found the archive (ls: cannot access /dev/sdf1: No such file or directory), but if I run this command, in console that show me the information.
What can I do?
That happen even running the php script as a root!!
$mountcommand = "ls -al /dev/$unit 2>&1";
exec("$mountcommand", &$mountoutput, &$mountresult);
print_r($mountoutput);
echo "\n$mountcommand\n\n";
This is in a forked process

Works for me.
The disks:
frayser#gentoo ~/doc/Answers/src/PHP $ disk
sda 111.790 GB
sdb 233.762 GB
sdc 233.762 GB
sdd 233.762 GB
sde 279.481 GB
------------------
Total: 1092.558 GB
Using hde:
frayser#gentoo ~/doc/Answers/src/PHP $ l /dev/sde
brw-rw---- 1 root disk 8, 64 Dec 2 13:36 /dev/sde
Using PHP:
frayser#gentoo ~/doc/Answers/src/PHP $ php lsdev.php
Z: brw-rw---- 1 root disk 8, 64 Dec 2 13:36 /dev/sde
The script:
frayser#gentoo ~/doc/Answers/src/PHP $ cat lsdev.php
<?php
$z=exec("ls -lr /dev/sde");
print "Z: " . $z . "\n";
?>
Update
It also works with ls -al.
Please show your script.
Update 2
Along the lines of the chroot suggestion by ircmaxell: Supposing that /dev, as seen in the PHP process, was a special chroot facsimile of the real one. And supposing that /dev/sdf1 is a removable device. The when sdf1(the media) is inserted the system automatically creates the device in the real /dev; but the chroot version isn't updated. So, to detect this situation look for differences between /dev as seen from PHP and from the commandline.
One test is ls -id on /dev(from PHP and the commandline). This prints the inode of /dev. Is there a mismatch in inode numbers?
Is /dev/sdf1 removable? Is it mounted? If it is mounted; does the PHP process see the mounted filesystem: ls $mount_point from PHP it.
There are other chroot tests listed on Stack Overflow and elsewhere; but I haven found many good ones: If a chroot is done correctly, it is hard to detect.
Update 3
This may be what is happening: The device (/dev/sdf1) takes a while to appear after it is created; so it is necessary to pause between creating the device and attempting to mount it:
Before: The device is created; but it isn't there...
Array
(
[0] => Logging out of session [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[1] => Logout of [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[4] => ls: cannot access /dev/sdg: No such file or directory
)
{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; ls -al /dev/sdg ;} 2>&1
After: Added a 1-second pause, and the device (sdg) is available...
Array
(
[0] => Logging out of session [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[1] => Logout of [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
[3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
[4] => brw-rw---- 1 root disk 8, 96 Dec 18 05:27 /dev/sdg
)
{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; sleep 1; ls -al /dev/sdg ;} 2>&1
The PHP with the pause ...
<pre>
<?php
$unit='sdg';
$reset="test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u";
$connect="sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l";
$test="ls -al /dev/$unit";
$mountcommand = "{ $reset; $connect; sleep 1; $test ;} 2>&1";
exec("$mountcommand", $mountoutput, $mountresult);
print_r($mountoutput);
echo "\n$mountcommand\n\n";
?>
</pre>
The /etc/sudoers is configured the same as reported in the previous question about mount failing from PHP.
So add a second or two of sleep() after iscsiadm creates the device. The sleep can be done in PHP instead of in the exec() of the shell.

I fix it with a sleep between this code and the code that gave me the "unit"

Related

Unable to execute system commands from PHP

I am trying to execute a couple of scripts by using a remote interface. The environment is Raspbian on a Raspberry Pi (although I will be using Debian later as well) running LAMP.
The files are test.php and test.sh in the root directory of the webserver (say example.com)
test.sh
#!/bin/bash
sudo pkill chromium-browse
sudo reboot
test.php
<?php
$output=null;
$resultCode=null;
exec("./test.sh", $output, $resultCode);
// $ouptut = shell_exec('./test.sh 2>&1'); //tried this too
// echo shell_exec("./test.sh"); // as well as this
echo "Returned with status $resultCode and output:\n";
print_r($output);
?>
Initially, I had used
chmod u+x test.sh
but got an error code of 126. So I did this:
chmod 777 test.sh
Now I get an error code of 1, but it still doesn't execute. I have also tried
sudo visudo
then added
pi ALL=(ALL) NOPASSWD: ALL
(pi is the current loggedin user)
Currently I am getting this:
Array
(
[0] =>
[1] => We trust you have received the usual lecture from the local System
[2] => Administrator. It usually boils down to these three things:
[3] =>
[4] => #1) Respect the privacy of others.
[5] => #2) Think before you type.
[6] => #3) With great power comes great responsibility.
[7] =>
[8] => sudo: no tty present and no askpass program specified
)
Note: I use sudo all the time at the command line without being asked for a password.
I do have another php file in the same directory that executes an actual system command successfully. It has this line:
$uptime = exec("uptime");
which works just fine, so I know system commands are possible. Is there any way to do this? I have seen other similar questions on SO and other sites, but none of those answers have worked for me.
Any help appreciated.

PHP Exec sudo mount throwing fancy error message

I'm using Centos7 OS, I have this script that mount a folder from windows PC
exec('sudo mount -t cifs -o username="'.$user.'",password="'.$pass.'",dir_mode=0777,file_mode=0777
//'.$ip_address.'/c/kitpos/update /mnt 2>&1', $output, $results);
And I got this fancy error message that looks like a permission issue.
Array
(
[0] =>
[1] => We trust you have received the usual lecture from the local System
[2] => Administrator. It usually boils down to these three things:
[3] =>
[4] => #1) Respect the privacy of others.
[5] => #2) Think before you type.
[6] => #3) With great power comes great responsibility.
[7] =>
[8] => sudo: no tty present and no askpass program specified
)
My /mnt folder ownership and permission is root and 777 but even if I change it www-data:www-data which is my php running as, still got the same error.
777 drwxrwxrwx 2 root root 4096 mnt
I also have an Ubuntu server but it works fine in their with that same code. Does anyone have an Idea?

Running two linux commands via PHP, don't wait for first to end before executing second

This is pretty weird, and I searched and tried everything, but I think I'm just making a dumb syntax error here.
I'm trying to run a stress test on the CPU , then immediately limit it's cpu usage to 30% , all this via PHP. The test is also run under another user and with a specified name so it can be limited. The stress test starts fine, but I can see the PHP file still loading, and it ends the second the stress test ends.
Here's some of the ways I tried doing it
$output = exec('sudo runuser -l test -c "exec -a MyUniqueProcessName stress -c 1 -t 60s & cpulimit -e MyUniqueProcessName -l 30"');
$output = exec('sudo runuser -l test -c "exec -a MyUniqueProcessName stress -c 1 -t 60s > /dev/null & cpulimit -e MyUniqueProcessName -l 30"');
The whole purpose of this is because I am writing a script for a game hosting website, and I want to limit the resource consumption of each server to improve quality and not let someone hog all the resources.
Basically, instead of the stress test, a game server will run.
edit::
here's what I have now:
I need to run the stress under "test" , but the cpulimit under either sudo apache or root, because it requires special permissions. The stress still starts fine but it eats 99.9%
passthru('sudo runuser -l test -c "exec -a MyUniqueProcessName stress -c 1 -t 60s &" & sudo cpulimit -e MyUniqueProcessName -l 30 -i -z');
I can't see the cpulimit in the process list after doing this http://i.imgur.com/iK2nL43.png
Unfortunately, the && does more or less the opposite of what you want. :-) When you do A && B in Bash, that means, "Run command A and wait until it's done; if it succeeded, then run command B."
By contrast, A & B means, "Run command A and then immediately run command B."
So you're close to right in your command, but just getting tripped up by using two bash commands (should only need one) and the &&.
Also, did you try running each command separately, outside PHP, in two terminals? I just downloaded and built both stress and cpulimit (I assume these are the ones you're using?), ran the commands separately, and spotted a problem: cpulimit still isn't limiting the percentage.
Looking at the docs for stress, I see it works by forking child processes, so the one you're trying to CPU-limit is the parent, but that's not the one using the CPU. cpulimit --help reveals there's option -i, which includes child processes in what is limited.
This gets me to the point of being able to enter this in one terminal (first line shows input at the prompt; subsequent show output):
$> exec -a MyUniqueProcessName stress -c 1 -t 60s & cpulimit -e MyUniqueProcessName -l 30 -i
[1] 20229
MyUniqueProcessName: info: [20229] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
Process 20229 found
Then, in another terminal running top, I see:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
20237 stackov+ 20 0 7164 100 0 R 30.2 0.0 0:04.38 stress
Much better. (Notice that outside the Bash shell where you aliased it with exec -a, you will see the process name as stress.) Unfortunately, I also see another issue, which is cpulimit remaining "listening" for more processes with that name. Back to cpulimit --help, which reveals the -z option.
Just to reduce the complexity a bit, you could leave the alias off and use the PID of the stress process, via the special Bash variable $!, which refers to the PID of the last process launched. Running the following in a terminal seems to do everything you want:
stress -c 1 -t 60s & cpulimit -p $! -l 30 -i -z
So now, just change the PHP script with what we've learned:
exec('bash -c "exec -a MyUniqueProcessName stress -c 1 -t 60s & cpulimit -e MyUniqueProcessName -l 30 -i -z"');
...or, simpler version:
exec('bash -c "stress -c 1 -t 60s & cpulimit -p \$! -l 30 -i -z"');
(Notice the $ in the $! had to be escaped with a backslash, \$!, because of the way it's quoted when passed to bash -c.)
Final Answer:
Based on the last example you amended to your question, you'll want something like this:
passthru('bash -c "sudo -u test stress -c 1 -t 60s & sudo -u root cpulimit -p \$! -l 30 -i -z"');
When I run this with php stackoverflow-question.php, it outputs the following:
stress: info: [3374] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
stress: info: [3374] successful run completed in 60s
Process 3371 found
(The second two lines only appear after the PHP script finishes, so don't be mislead. Use top to check.)
Running top in another terminal during the 60 seconds the PHP script is running, I see:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3472 test 20 0 7160 92 0 R 29.5 0.0 0:07.50 stress
3470 root 9 -11 4236 712 580 S 9.0 0.0 0:02.28 cpulimit
This is exactly what you've described wanting: stress is running under the user test, and cpulimit is running under the user root (both of which you can change in the command, if desired). stress is limited to around 30%.
I'm not familiar with runuser and don't see the applicability, since sudo is the standard way to run a process as another user. To get this to work, you may have to adjust your settings in /etc/sudoers (which will require root access, but you obviously already have that). That's entirely outside the scope of this discussion, but as an example, I added the following rules:
my-user ALL=(test) NOPASSWD:NOEXEC: /home/my-user/development/stackoverflow/stress
my-user ALL=(root) NOPASSWD:NOEXEC: /home/my-user/development/stackoverflow/cpulimit

php linux regex how to match if cron process is running?

I want to run only one proccess at a time. So I need to check. I found there was suggestions to use exec()
so I made test functions - one which sleeps 1 minute and one which tests if process is running.
public function test($a='', $b='') {
exec("ps ax | grep 'php -q /var/www/glab/index.php ajax/test2'", $pids);
if (count($pids) > 2) {
$exists = true;
echo 'exists' . count($pids);
print_r($pids);
}
}
And I get result:
exists3Array
(
[0] => 30680 pts/8 S+ 0:00 php -q /var/www/glab/index.php ajax/test2
[1] => 30684 ? S 0:00 sh -c ps ax | grep 'php -q /var/www/glab/index.php ajax/test2'
[2] => 30686 ? S 0:00 grep php -q /var/www/glab/index.php ajax/test2
)
I did not expect 3 processes but I see its ok. Can I be sure that my function is working ok - detecting running when there is > 2, am I not missing something? For example maybe if some user will run some program on linux maybe this will not work anymore?
Or can you sugesst some check which matches only the one process, without sh and grep? I mean exact string. I was trying but cannot make it work to match only one which I am searching.
Edit:
googled bit more and found more examples, adjusted and have this:
exec ('ps -efa | grep "php -q /var/www/glab/index.php ajax/test2" |grep -v "grep " | awk "{print $10 $NF}"', $pids);
print_r($pids);
When process runs:
Array
(
[0] => darius 2046 12877 5 09:23 pts/8 00:00:00 php -q /var/www/glab/index.php ajax/test2
)
It matches now 1 processs. Could you check if this is ok, am I not missing something?

run ffmpeg from PHP web script

I need to manage the recording/capture of a website mindwhile it is running a slide-show to get videos form these slides.
My approach is:
<?php
define('FFMPEG_LIBRARY', '/usr/bin/ffmpeg ');
$ffmpegcmd = "ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg";
shell_exec($ffmpegcmd);
?>
But i get this error from php error log:
[x11grab # 0x81e8aa0] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 800 height: 600
No protocol specified
No protocol specified
[x11grab # 0x81e8aa0] Could not open X display.
:0.0: Input/output error
Similar command from console run good.
Please, any help to get display and be able to control ffmpeg from browser php script?
Thanks in advance.
thanks for your time.
I got rid the X display error, but not I still haven't got the capture.
Using xvfb I get an unknown file at /tmp written by www-data user:
-rw-r--r-- 1 www-data www-data 11252 Sep 12 09:49 server-B20D7FC79C7F597315E3E501AEF10E0D866E8E92.xkm
Running startx I got also an unknown file at /tmp
-rw------- 1 www-data www-data 59 Sep 12 09:53 serverauth.oLcFlG7tXC
any of both grow in size so it is not capturing anything. The content is some binary thing.
What are those files about?
What I am trying is to write a script in which I can control the time ffmpeg is capturing the desktop to create a video from a jquery slide displayed on a website.
My try from console is closer, but if I can do it by browser I will be able to know when to stop sending an AJAX request once the slide is finished.
This is my try from console:
#!/bin/bash
# start the slide website: I will need to change it to control by querystring the language and course level
firefox http://www.languagecourse.net/vocabulary-trainer.php &
# start recording: I will need to adjust the frame to capture
ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output2.mpg &
# since I can't control the time a course takes I pause an arbitrary time
sleep 5
# look for the capture PID and close it
for i in $(ps aux | grep ffmpeg | sed "s/ */#/g" | cut -f2 -d#)
do
echo "proceso $i killed"
kill -9 $i
done
I wonder once the website is opened I can continue doing the control from AJAX, but not know if I will be able to get the ffmpeg PID to stop the command.
I will appreciate any kind of comments.
Regards,
ยท_-
You can use Xvfb to emulate a x-environment
<?php
$ffmpegcmd = "xvfb-run -a ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg";
shell_exec($ffmpegcmd);
or something like this
<?php
$ffmpegcmd = "startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 && ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg");
shell_exec($ffmpegcmd);
That should be good to get rid of the "Could not open X display." error, and will probably solve your problem.

Categories