I'm using exec to run a background script like this :
$command = "/usr/local/bin/php public_html/r/index.php tools $action process $params > /dev/null &";
exec($command);
The thing is : it's NOT working.
Hints :
When I'm executing the very same command from the terminal (via SSH) it's working fine.
The exec command is enabled (I can execute any command without issue).
Any ideas?
background jobs tend to have different 'current' directories than your shell - usually it's the home directory of the account that the job is running under. Unless your public_html is in /home/whoever, you're not actually running your script. Try an absolute path:
$command = "/usr/local/bin/php /path/to/public_html/r/index.php etc..."
^^^^^^^^^
instead.
Related
I have a php web application and on one of the route I try to execute node script.
Node script location: C:\Users\meusr\Workspace\www\myproject\directImport.js
I have configured my package.json, such that npm start executes above script.
php (laravel) application's executable index.php located at: C:\Users\meusr\Workspace\www\myPHPproject\public\
And on one of my route: importer/run
I have:
$commandToRun = "npm start --prefix ". env('IMPORTER_PATH'). " /dev/null 2>&1";
Where IMPORTER_PATH is configured in my env, as: IMPORTER_PATH = C:\Users\meusr\Workspace\www\myproject\
when I try to execute the command with exec
exec($commandToRun, $output);
Page keeps on loading no output, no error and no end.
The script has no issue and running the same command on command line on windows system works:
npm start --prefix C:\Users\meusr\Workspace\www\myproject\
outputs as expected.
I thought it was issue with permission first (which should have thrown error, but I still tried and ran another command)
exec('npm -v', $output);
which outputs my npm version.
Similarly I tried to use simply node directImport.js which didn't work either.
Then tried to change directory to the location where node file is located and ran the command again.
exec(cd C:\Users\meusr\Workspace\www\myproject\ && dir /dev/null 2>&1) // this works, but:
exec(cd C:\Users\meusr\Workspace\www\myproject\ && node directImport.js /dev/null 2>&1) // this didn't work
Thanks to miken32, what worked for me was:
<?php
$commandString = 'start /b c:\\programToRun.exe -attachment "c:\\temp\file1.txt"';
pclose(popen($commandString, 'r'));
?>
http://php.net/manual/en/function.popen.php#92413
Just migrated a website from a linux server without a cpanel to a server with a cpanel. My problem is when I hit start to start a process which executes a php file. It will not run the file. If I run the file from terminal everything works.
The code that calls the file.
if ($do === "start_service") {
create_marker_file("$service_running_marker");
system_bg("php $dir/$service_script");
$message = "Started Service!";
$running = true;
$status = "Running";
}
The systembg is a function.
function system_bg($command) {
exec('bash -c "exec nohup setsid ' . $command . ' > /dev/null 2>&1 &"');
}
If i do ps ax | grep service.php in terminal is see this
/opt/cpanel/ea-php70/root/usr/bin/php-cgi /home/xsocial1/public_html/xsmp/service.php
When I run the file in terminal this is what i see when I run ps ax | grep service.php
/opt/cpanel/ea-php70/root/usr/bin/php service.php
I think my problem is I need the server to call a cli call and not a cgi call
This is correct, you will need to figure out the CLI PHP path and use that to start the process. This is sadly not exactly standardised, but on a *nix machine you will usually have the right one by using PHP_BINDIR .'/php' (PHP_BINDIR being a PHP constant).
You may try to check the webserver user has a privilegies to run the bash.
I am using this code on Ubuntu 13.04,
$cmd = "sleep 20 &> /dev/null &";
exec($cmd, $output);
Although it actually sits there for 20 seconds and waits :/ usually it works fine when using & to send a process to the background, but on this machine php just won't do it :/
What could be causing this??
Try
<?PHP
$cmd = '/bin/sleep';
$args = array('20');
$pid=pcntl_fork();
if($pid==0)
{
posix_setsid();
pcntl_exec($cmd,$args,$_ENV);
// child becomes the standalone detached process
}
echo "DONE\n";
I tested it for it works.
Here you first fork the php process and then exceute your task.
Or if the pcntl module is not availabil use:
<?PHP
$cmd = "sleep 20 &> /dev/null &";
exec('/bin/bash -c "' . addslashes($cmd) . '"');
The REASON this doesn't work is that exec() executes the string you're passing into it. Since & is interpreted by the shell as "execute in the background", but you don't execute a shell in your exec call, the & is just passed along with 20 to the /bin/sleep executable - which probably just ignores that.
The same applies to the redirection of output, since that is also parsed by the shell, not in exec.
So, you either need to find a way to fork your process (as described above), or a way to run the subprocess as a shell.
My workaround to do this on ubuntu 13.04 with Apache2 and any version of PHP:
libssh2-php, I just used nohup $cmd & inside a local SSH session using PHP and it ran it just fine the background, of course this requires putting certain security protocols in place, such as enabling SSH access for the webserver user, so it would have exec-like permissions then only allowing localhost to login to the webserver ssh account.
I am trying to Execute a multiple commands in php using exec() and shell_exec but i am getting a null value back which i shouldn't and nothing is happening (if i copy and paste the strings below in the command line it will work fine and accomplish the job needed) this is the commands i am using:
$command = "cd /../Desktop/FolderName;";
$command .= 'export PATH=$PATH:`pwd`;';
$command .= 'Here i execute a compiler;';
and then i use the escapeshellcmd()
$escaped_command = escapeshellcmd($command);
then
shell_exec($escaped_command);
any ideas what i am doing wrong and i also tried escapeshellarg() instead of escapeshellcmd()?
Solution: the Problem was the permission of the execution compiler for other owners is non and this was the problem.
because when you are using exec() function in php the owner of the file will be www-data so you need to give permission for the www-data either from the ACL of ubuntu or whatever linux based operating system(you can know the owner by doing this exec('whoami')), or by the files you need to execute.
(Sorry my bad English)
On Linux you can add your Commands in a Shell Script.
You can put this in any file:
#!/bin/bash
cd /../Desktop/FolderName
export PATH=$PATH:`pwd`
EXECUTE COMPILER
And save this as fille.sh
Then, add execution permissions:
chmod +x path/to/file.sh
From PHP, you can call this Script executing:
shell_exec('sh path/to/file.sh');
Hope this helps!
Im new to php shell commands so please bear with me. I am trying to run the shell_exec() command on my server. I am trying the below php code:
$output = shell_exec('tesseract picture.tif text_file -l eng');
echo "done";
I have the picture.tif in the same directory as the php file. In my shell I can run this without a problem.
It takes a while to run the code, then it doesnt make the text_file like it does when I run it in command prompt.
Per your comment:
Should I write a loop in shell
instead?
You can write a very simple shell script to run the command in a loop. Create the script file first:
touch myscript.sh
Make the script executable:
chmod 700 myscript.sh
Then open it with a text editor such as vim and add this:
for (( i = 0 ; i <= 5; i++ ))
do
tesseract picture.tif text_file -l eng
done
Thats the very basics of it (not sure what else you need), but that syntax should help get you started. To run the script, do this if you're in the same directory as the script:
./myscript.sh
Or specify the full path to run it from anywhere:
/path/to/mydir/myscript.sh
Could this be a permissions issue? My guess is that PHP isn't running with the same permissions that you do when you execute the command directly from the command prompt. What OS are you running on?