Use PHP over HTTP to run background bash with trap on exit

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Use PHP over HTTP to run background bash with trap on exit



I'm trying to use PHP to trigger a bash script that should never stop running. It's not just that the command needs to run and I don't need to wait for output, it needs to continue running after PHP is finished. This has worked other times (and the question has been asked already), the difference seems to be my bash script has a trap for when it's closed.



Here is my bash script:


#!/bin/bash

set -e

WAIT=5
FILE_LOCK="$1"

echo "Daemon started (PID $$)..."

echo "$$" > "$FILE_LOCK"

trap cleanup 0 1 2 3 6 15

cleanup()

echo "Caught signal..."
rm -rf "$FILE_LOCK"
exit 1


while true; do
# do things
sleep "$WAIT"
done



And here is my PHP:


$command = '/path/to/script.sh /tmp/script.lock >> /tmp/script.log 2>&1 &';
$lastLine = exec($command, $output, $returnVal);



I see the script run, the lock file get created, then it exits, and the trap removes the lock file. In my /tmp/script.log I see:


/tmp/script.log


Daemon started (PID 55963)...
Caught signal...



What's odd is that this only happens when running the PHP via Apache. From command line it keeps running as expected.



The signal on the trap that's being caught is 0.


0



I've tried wrapping my command in a bash environment, like $command = '/bin/bash -c "' . addslashes($command) . '"';, also tried adding nohup to the beginning. Nothing seems to be working. Is this possible to do for a never ending script?


$command = '/bin/bash -c "' . addslashes($command) . '"';


nohup





can't you set a flag with the php script and have the bash script check it on startup and have the bash script run on a cron trigger?
– Tschallacka
Aug 8 at 18:56





So... your PHP script times out when invoked over the web but doesn't over CLI because there are two totally separate configurations for PHP in those contexts and likely set_time_limit is different? Am I missing something?
– ficuscr
Aug 8 at 19:47



set_time_limit





@ficuscr the PHP script doesn’t time out, as it triggers a background process and exits immediately.
– lxg
Aug 8 at 19:50






@SteveRobbins: Are you sure the # do things part doesn’t cause any trouble? Can you try removing it and echo’ing some string instead? Does it go to the log file?
– lxg
Aug 8 at 19:52


# do things





Wow I misread most of that first time. What Igx is is saying.. ^ maybe an uncaught trap that you only observe as a 0? I'm hung up on why it behaves differently depending on PHP interpreter context.
– ficuscr
Aug 8 at 20:05




1 Answer
1



Found the problem thanks to @lxg.



My # do things command was giving errors, which was causing the script to exit. For some reason they were suppressed.


# do things



When removing set -e from the beginning of my bash script I started seeing the errors output to my log file. Not sure why they didn't show up before.


set -e



The issue was in my bash loop it was running PHP commands. Even though my bash user and Apache user are the same, for some reason they had different $PATHs. This meant that when running on command line I was using a PHP7 binary, but when Apache trigged bash commands it was using a PHP5 binary (even though Apache itself is configured to use PHP7). So the application errored out and that is what caused the script to die.


$PATH



The solution was to explicitly set the PHP binary path in my bash loop.



I was doing this with


BIN_PHP=$(which php)



But on true command line it would return one value (/path/to/php7/bin/php) vs command line initiated by Apache (/path/to/php5/bin/php). Despite Apache being the same as a my command line user, it didn't load the ~/.bashrc which specified my correct PHP path.


/path/to/php7/bin/php


/path/to/php5/bin/php


~/.bashrc






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS