I have an Shell script that runs a PHP script every 8 seconds as seen below:
enter code here
#!/bin/bash
while true; do
php /var/www/html/folder1/scripts/processtask.php /var/www/html/folder1/app
sleep 8;
done
enter code here
I need it to continue this way, however I need it to stop running at 2am until 3am then resume after that on a daily basis
You can use cronetab for the same. Check the below link for configuration:
https://www.baeldung.com/linux/schedule-script-execution
If you only want to start the script if not between 2am and 3am (but an allready started script does not need to be stopped) this could be easily checked with an additional if-statement:
#!/bin/bash
while true; do
if [[ 2 -le $(date '+%H') && $(date '+%H') -le 3 ]]; then
sleep 60
else
php /var/www/html/folder1/scripts/processtask.php /var/www/html/folder1/app
sleep 8
fi
done
Related
right now I'm using this script for a program:
export FREESURFER_HOME=$HOME/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh
cd /home/ubuntu/fastsurfer
datadir=/home/ubuntu/moya/data
fastsurferdir=/home/ubuntu/moya/output
mkdir -p $fastsurferdir/logs # create log dir for storing nohup output log (optional)
while read p ; do
echo $p
nohup ./run_fastsurfer.sh --t1 $datadir/$p/orig.nii \
--parallel --threads 16 --sid $p --sd $fastsurferdir > $fastsurferdir/logs/out-${p}.log &
sleep 3600s
done < /home/ubuntu/moya/data/subjects-list.txt
Instead of using sleep 3600s, as the program needs around an hour, I'd like to use wait until all processes (several PIDS) are finished.
If this is the right way, can you tell me how to do that?
BR Alex
wait will wait for all background processes to finish (see help wait). So all you need is to run wait after creating all of the background processes.
This may be more than what you are asking for but I figured I would provide some methods for controlling the number of threads you want to have running at once. I find that I always want to limit the number for various reasons.
Explaination
The following will limit concurrent threads to max_threads running at one time. I am also using the main design pattern so we have a main that runs the script with a function run_jobs that handles the calling and waiting. I read all of $p into an array, then traverse that array as we launch threads. It will either launch a thread up to 4 or wait 5 seconds, once there are at least one less than four it will start another thread. When finished it waits for any remaining to be done. If you want something more simplistic I can do that as well.
#!/usr/bin/env bash
export FREESURFER_HOME=$HOME/freesurfer
source $FREESURFER_HOME/SetUpFreeSurfer.sh
typeset max_threads=4
typeset subjects_list="/home/ubuntu/moya/data/subjects-list.txt"
typeset subjectsArray
run_jobs() {
local child="$$"
local num_children=0
local i=0
while [[ 1 ]] ; do
num_children=$(ps --no-headers -o pid --ppid=$child | wc -w) ; ((num_children-=1))
echo "Children: $num_children"
if [[ ${num_children} -lt ${max_threads} ]] ;then
if [ $i -lt ${#subjectsArray[#]} ] ;then
((i+=1))
# RUN COMMAND HERE &
./run_fastsurfer.sh --t1 $datadir/${subjectsArray[$i]}/orig.nii \
--parallel --threads 16 --sid ${subjectsArray[$i]} --sd $fastsurferdir
fi
fi
sleep 10
done
wait
}
main() {
cd /home/ubuntu/fastsurfer
datadir=/home/ubuntu/moya/data
fastsurferdir=/home/ubuntu/moya/output
mkdir -p $fastsurferdir/logs # create log dir for storing nohup output log (optional)
mapfile -t subjectsArray < ${subjects_list}
run_jobs
}
main
Note: I did not run this code since you have not provided enough information to actually do so.
I have a script to which I wanna add a shutdown timer at the end, I'd like the countdown to run in a new terminal windows so I can cancel it since the script will usually be run in the background.
Here's the problem,
a simple script containing only the following
secs=$((60))
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
shutdown now
works fine, but if I try to send it to a new terminal like this
gnome-terminal -e "bash -c
'secs=$((60))
while [ $secs -gt 0 ]; do
echo -ne \"$secs\033[0K\r\"
sleep 1
: $((secs--))
done
shutdown now'"
it fails and just shuts down. If I remove the shutdown line I get this error :
Option ā-eā is deprecated and might be removed in a later version of gnome-terminal.
Use ā-- ā to terminate the options and put the command line to execute after it.
Does anyone know how I could fix this?
thanks
The easy way to do this is to export a function:
countdown() {
secs=60
while (( secs > 0 )); do
printf '%s\033[0K\r' "$secs"
sleep 1
((secs--))
done
shutdown now
}
export -f countdown
gnome-terminal -- bash -c countdown
I'm working on a bash script that will run for appoximately 30 minutes at a time. I've got it running stable as far as that part goes. I've been looking for a way to make it fire certain commands at inervals of every 3 minutes while running. I've not had any luck, so I turn to those of you that may know more about bash than I.
Any suggestions?
Here is what I have in mind of doing.
START=$(date +%s);
while read LINE <&3; do
END=$(date +%s);
if [[ $(($END-$START)) > 180 || $(($END-$START)) == 180 ]]
then
$START=$(date +%s);
run command
fi
done
Add a cron job to make it run every 3 minutes.
*/3 * * * * /path/to/script
What about the watch command?? (https://unix.stackexchange.com/questions/10646/repeat-a-unix-command-every-x-seconds-forever)
(Second answer on here: Run command every second)
You can run a loop in the background:
{ while /bin/true; do some_command; sleep 180; done; } &
loop_pid=$!
Then before the main script exits, kill the background loop:
kill $loop_pid
You can also call the same script from same script.
$ cat script.sh
#!/bin/bash
# commands
# commands
sleep 1800
sh $0
I was hoping somebody would be able to help me with this
I need a loop for a shell script that will run what is inside the loop for 15 seconds. SO for example
if (true)
run command for 15 seconds
fi
kill PID
I am new to shell scripting, so i am lost with this.
Also I am using a debian instll if that makes any difference
Any help is appreciated
Are you looking for the timeout command?
The following bash script might work for you. The script will set the initial epoch time as a variable prior to beginning a loop. While the loop runs an additional variable will be set with the current epoch time. Both epoch times will be compared and as long as the difference is less than or equal to 15 your command will continue to run. Note that in the script below the current command running is 'echo "counting ${COUNTER}"'. You should change this portion of the script to match what you are trying to accomplish. Once the difference of the two epoch times is greater than 15 the script will exit. You will need to initate your kill command at this point. If an error does occur you should see "ERROR... YourScript.sh failed" in "YourLogFile" (set your log file to what you would like)
NOTE: Whatever you are attempting to run while inside this loop may run many many many times within the 15 second period. By utilizing the script below as a test you will see that the echo command runs more than 50 times per second.
#!/bin/bash
LOOP="true"
INITIAL_TIME=$(date "+%s")
while [[ ${LOOP} == true ]]; do
CURRENT_TIME=$(date "+%s")
COUNTER=$(expr ${CURRENT_TIME} - ${INITIAL_TIME})
if [[ ${COUNTER} -le "15" ]]; then
echo "counting ${COUNTER}"
# RUN YOUR COMMAND IN PLACE OF THE ABOVE echo COMMAND
elif [[ ${COUNTER} -gt "15" ]]; then
exit 0
#INITIATE YOUR KILL COMMAND IN PLACE OF OR BEFORE THE exit
else
echo "ERROR... YourScript.sh failed" >> /YourLogFile
fi
done
I have a bash script in Ubuntu, I want it to run every 10 minutes for example after it's done. How can I do this? Thanks!
You can check watch.
From the man pages of watch the description says watch - execute a program periodically, showing output fullscreen, you can try watch -n 600 my_script.sh which will execute myscript.sh every 600 seconds i.e. 10 minutes. watch shows the output to full screen, you can redirect it to say /dev/null in case you are not interested in the output to the screen.
Hope this helps!
Cronjobs is what you need.
My blog post:- http://linux-junky.blogspot.com/2010/10/guide-to-add-cronjob-simplified.html
Or you can also use sleep 600 in your script.
You can use at to reschedule the script from within the script. At the end of the script put:
at now + 10 minutes << END
"$0" "$#"
END
Or with crontab -e, or another option, to check the date. for example, if you want to do something every 10 minutes, you can write:
if [ $((`date +%M`%10)) -eq 0 ] && [ `date +%S` -lt 10 ]; then
#your code
fi