I'm trying to run a script from a PHP on my server.
<?php exec("sh /var/www/scripts/script.sh", $output); ?>
my script is:
#!/bin/bash
sh Stop.sh
nohup java -jar RestApiRandonSender.jar& # You send it in background
MyPID=$! # You sign it's PID
echo $MyPID # You print to terminal
echo "kill -9 $MyPID" > Stop.sh
echo "rm Stop.sh" >> Stop.sh
My folder structure is:
/var/www/
/html/restart.php
/scripts/script.sh
all group and users are root
.
When I access the PHP page from Chrome (localhost/restart.php
), nothing happens.
But when I'm in the folder scripts and I run
sudo php ../html/restart.php
the scripts restarts the jar file and creates the Stop.sh
, but the script doesn't end until I press Ctrl+c or Ctrl+z.
I'm new to Linux. I hope I gave all the details needed..
EDIT: I changed the path to the full file path in script.sh and now it is almost working.
The process still never ends. The webpage from chrome is waiting for response and the process id of the jar file is completely different from the process id I get in the Stop.sh
$ ps -ef | grep java
apache 10086 1 76 11:23 ? 00:05:00 java -jar /var/www/scripts/RestApiRandonSender.jar