Execute at command from php page with command line input - php

I want to execute the below command from PHP shell_script in Linux environment.
shell_exec('at 12:39 <<< "mkdir newfolder"');
I have tried in all the method of PHP to execute this script but it is not working.
In terminal when I am running at 12:39 <<< "mkdir newfolder" it is executing and the task is scheduling.But when I am trying the same script to run in php by using shell_exec it is not working.
You can understand the issues when you type at 12:39 it will show you to put the input and press Ctrl+d to complete execution. to make this in a single line here I am using <<<
Can anyone suggest how can I execute this script from the PHP?

shell_exec uses dash shell system by default : to make sure run php -r 'echo shell_exec("echo $0");' and it will output 'sh' , and Dash does not have the <<< redirection operator.
Instead you could force the use of Bash and do something like:
shell_exec('/bin/bash -c \'at 12:39 <<< "mkdir newfolder"\'');
Hope this will help.

Related

Why update nohup.out when running nohup in exec php

I'm running a php socket. I run the program through nohup. Run this program properly through root. But my problem is running the program via the exec () function in php. When I run the command this way the program runs correctly but the program output is not printed in nohup.out.
my command in ssh:
nohup php my_path/example.php & #is working
my command in user php:
exec('nohup php my_path/example.php >/dev/null 2>&1 &', $output); #not update nohup.out
please guide me...
From PHP docs on exec:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
From man nohup:
If standard input is a terminal, redirect it from /dev/null. If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use 'nohup COMMAND > FILE'.
To satisfy both - redirect manually to nohup.out:
exec('nohup php my_path/example.php >>nohup.out 2>&1 &', $output);

Attempting to launch a php scrip in the background from a php script, using the exec() function, on a raspberry pi

I want to have a chat bot working on my raspberry pi.
In the command line, I am entering this command:
nohup php /path/to/script.php >/dev/null 2>&1 &
And this works. When I decide I need the the bot to be down, I enter this command:
killall php
This works, but I want to be able to start and stop the script from a browser.
So, in my dashboard php script....
//This command works.
$runningProc = exec("pgrep -a php");
//This command does not. The process starts, but immediately ends
$startCommand = "nohup php /path/to/script.php >/dev/null 2>&1 &";
exec($startCommand);
//This command does not seem to work for a nohup started from the command line.
exec("killall php");
Okay, so I figured out what was going on, once I found some relevant logs.
In my dashboard file I changed the exec() argument to :
$startCommand = "nohup php script.php >/dev/null 2> nohup.out &";
exec($startCommand);
This way, I could review the 'nohup.out' file for any errors.
The first require() command triggered a "No such file" error. Changing that function to the full filepath made everything work as expected.

Execute multiple sage commands from php script

I have installed Sage math on my ubuntu server. I can run sage commands by ssh terminal such as
$sage:
$sage: f = x^2
$sage: f.diff(x)
I would like to do on a php script
exec('sage');
exec('sage: f = 5x^3');
$fprime = exec('sage: latex(f.diff(x))');
echo $fprime;
I would expect "15x^2" as output but that's not happening .. however on ssh terminal all is good..
all help would be greatly appreciated..
I'm not completely sure of what you want to achieve. PHP's exec function executes a shell command, and that's all. Read http://fr2.php.net/manual/en/function.exec.php.
sage: f = 5*x^3 is not a shell command. You can run sage scripts directly via the command line using the -c switch :
./sage -c 'print latex((5*x^3).diff())'
will print 15 \, x^{2} to the terminal. In PHP you can grab this output by passing a second argument to exec:
exec("./sage -c 'print latex((5*x^3).diff())'", $output);
echo $output[0];

Executing multiple line shell command in php

I'm trying to execute a shell command from php using the exec command
The following command works
echo exec('whoami');
but I need to execute the following shell command
cat > /var/www/myfolder/abc.txt
hi
hello
welcome
the problem is that the above shell command has to be executed in multiplelines. The words "hi", "hello", "welcome" are dynamic and come from php variables. Is there a workaround for this problem.
To execute a multiline shell command in php:
passthru("bash <<'END'
echo \"executed in new bash session\";
pwd;
whoami;
exit;
END
");
This way you could for example connect to a ssh server using php and then execute some commands there
I don't understand what you mean by cat > abc.txt. This command will empty the file.

php command line exec() multiple execution and directories?

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!

Categories