Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1332  / 3 Years ago, thu, august 5, 2021, 6:03:38

I have a php console app and an Ubuntu server. I'm executing the app with this basic command:



php app.php


I want to execute it repetitively, but cronjobs is not suitable for me. Instead, I want it to execute such that when app.php finishes working it will be executed again immediately (like an infinity loop), rather than being executed again every X minutes.
So I made this bash script:



while true; do
php app.php
done


this solves my problem, but the usage of while true seems evil. Is it possible to stop this bash script, if necessary? Or can you tell me a better way to do this?



(app.php file , fetches a long list of website URLs and their content. It's like a web spider. I connect to the server via ssh , execute bash script with nohup, and then close terminal. )


More From » bash

 Answers
2

Here is an example about what you can do:



#!/bin/bash

while true; do
if [ -t 0 ]; then stty -echo -icanon time 0 min 0; fi

keypress=''
while [ "x$keypress" = "x" ]; do
#do something... in your case:
php app.php
#end of 'do something'

read keypress
done

if [ -t 0 ]; then stty sane; fi

echo "You pressed '$keypress' for pause."
read -p 'Press [Enter] key to continue or [Ctrl+C] to finish...'
done


To pause you can press any key without Enter. To continue you must to press Enter. To finish you must to press Ctrl+C.






If you run the script in background using:



nohup <name_of_script> &


you can stop it using the following commands:



ps -ef | grep <name_of_script> #to find the pid of the running script
kill -9 <pid_of_your_running_script>

[#29896] Friday, August 6, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rvousnove

Total Points: 456
Total Questions: 130
Total Answers: 98

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
;