1

Here is my bash script

#!/bin/bash
timestamp() {
  date +"%T"
}
pushd 'httpdocs/DIR1/DIR2'; 
file="Lock3.key"
if [ -f "$file" ]
then
    echo "$file found, Session is already in progress" $(date)
else
    echo "$file was not found, Its safe to start the script"  $(date)
    echo "Running Ebay Stock Only Script" $(date)
#Its possible to double check if the script is running by checking memory.
#ps aux | grep "php Cron-Run.php" (Not safe)
php Cron-Run.php > Output.log
echo "Finished Running Script, Lock file is deleted by PHP, Now exiting" $(date)
fi
exit

This is run from a cron job, and works. But all I want is the last echo to only run when the PHP process has finished (it will run for a hour) and, when the email comes through, it would be nice to see the output of the last echo with the associated time.

At the moment, the echo runs two seconds after the PHP script is called.

  • 1
    This has nothing to do with bash then. The way you have written it, it should wait until the script exits. Presumably, your php script is configured to be run in the background. Can you show us the contents of the PHP script? By the way, to check if it is running, use pgrep -f Cron-Run.php instead. – terdon Mar 2 '16 at 15:21
  • Its a collecton of 34 Files all in all. the file i am attempting to execute is copied here if you want to view it: pastebin.com/m8JzWhSN I cannot past in the whole source to the project. I thought there was a way of looking for PID created from within Bash, then i could Check to see if the Process is there before Echoing out its finished. – Dave Hamilton Mar 2 '16 at 15:42
  • 1
    There is, but the details will depend on what exactly you are doing. Try to edit your question and give us a minimal reproducible example. Something that displays the behavior you want to fix and with which we can play. If you just want to see if a process is running, you can so pgrep -f processName. Just be sure that is unique. Again, we can help with all this if you edit and make what you need clear. – terdon Mar 2 '16 at 15:49
  • between PHP Cron-Run...... And Echo Can i not use a WAIT and look for the process, as this works on Background processes, and i only want to wait for Child Spawned processed to finish before printing its finished. – Dave Hamilton Mar 2 '16 at 15:51
  • I don't know what that's supposed to be. As I told you, the reason why echo is being run immediately is in your PHP script. We can't help unless you give us an example of it. If you want this question to be about how you can check whether another process is running and only execute a command if it is not, please edit your question so it asks that instead. – terdon Mar 2 '16 at 15:53
0

I was able to pause / wait for the process to finish by using the wait function.

an example of the working segment:

#!/bin/bash
timestamp() {
  date +"%T"
}
pushd 'httpdocs/DIR1/DIR2'; 
file="Lock3.key"
if [ -f "$file" ]
then
    echo "$file found, Session is already in progress" $(date)
else
    echo "$file was not found, Its safe to start the script"  $(date)
    echo "Running Ebay Stock Only Script" $(date)
#Its possible to double check if the script is running by checking memory.
#ps aux | grep "php Cron-Run.php" (Not safe)
php Cron-Run.php > Output.log
wait
echo "Finished Running Script, Lock file is deleted by PHP, Now exiting" $(date)
fi
exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.