How to schedule a task automatically with a while loop in bash? - linux

I'm trying to schedule my script to run at two different times and then keep running in the background waiting for the next time that the conditions are met again.
So far I am not able to do it because after that one condition is met this scripts exits automatically:
function schedule {
while :
do
hour=$(date +"%H")
minute=$(date +"%M")
if [[ "$hour" = "02" && "$minute" = "31" ]]; then
# run some script
exec /home/gfx/Desktop/myscript.sh
wait
schedule
elif [[ "$hour" = "02" && "$minute" = "32" ]]; then
# run some script
exec /home/gfx/Desktop/myscript.sh
wait
schedule
fi
done
}
schedule
The script that I execute is the following :
$cat myscript.sh
echo "this a message"
Any idea or comment are welcome, thanks!

When you do:
exec some program
something else
if the exec works, then the something else will never happen because the script is replaced by what you exec'd
Maybe instead of:
exec /home/gfx/Desktop/myscript.sh
wait
schedule
you want:
/home/gfx/Desktop/myscript.sh &
wait
# schedule -- don't make a fork bomb
As noted in the comments, & + wait is kind of silly, you probably want:
/home/gfx/Desktop/myscript.sh &
# wait
# schedule
or:
/home/gfx/Desktop/myscript.sh
# wait
# schedule
depending on whether or not you want myscript in the background.
ALSO you should put a sleep in the loop, (maybe sleep(59)) to keep it from looping like a banshee.
You begin to understand the appeal of cron I think (at least for things you don't want to interact with your terminal session).

Crontabs are best place to put these things.
Edit crontab using
$ crontab -e
Add following lines to the files
31 2 * * * /home/gfx/Desktop/myscript.sh
And save crontab file. Your script will be executed every day 2:31 am

Related

How to call another shell script from a shell script in Linux

I am trying to create one wrapper script to trigger the parent script at 6AM only. But it is not properly working. It always showing Do Nothing and else part is looping without exit. Can any one help?
I want to check if the time is 6 AM and also need to check the string from text file(status). if both conditions are satisfied my parent script should execute via this wrapper script.
Below is the script
path= $(cat /home/avj/)
mode= $(cat /home/avj/status)
checktime="060000"
echo $checktime
echo $currentTime
while true
do
currentTime=date +"%H%M%S"
if [[ $currentTime -eq $checktime && $status == running ]];
then
sh &path/parent.sh
else
echo Do Nothing
fi
done
Usually on Linux, you use crontab for this kind of scduled tasks. But you have to specify the time when you "set up the timer" - so if you want it to be configured in the file itself, you will have to create some mechanism for this.
For example, you can add crontab
30 1 * * 5 /path/to/script/script.sh
Will execute the script every Friday at 1: 30 (AM) here:
30 is minutes
1 is an hour
the next 2 * are the day of month and month (in that order), and the 5th is the day of the week

cron script needs conditional to run

I have two python scripts that Im trying to run on my server
I currently have process_one running through a cron job every five minutes I want to add the second script to the cron job.
I was told by the freelancer that both programs can run automatically by writing a shell script. If process_one generates data in its output_folder (i.e.
process_two's input_folder) then it will return system status "0" (OK) to the operating system, otherwise it returns a
ERROR signal - even in the case of "no errors, yet nothing new produced".
Im at a loss Ive never written shell scripts before. Im looking on here and else where but I dont know how to write this. Any help would be appreciated
/path/to process1/process_one && /path/to process2/process_two
What you have for the cron job is correct, process one will run and if successful process_two will run. The only thing that is missing is for process_two to check for new data in the process_one output directory before it runs.
I would suggest that you use a few lines at the top of process_two python script to check for recent data and exit if not found. Either you can modify the python script itself to do this, or write a bash wrapper as process_two that simply calls the python script after checking for recent data.
py
import os.path, time
fileCreation = os.path.getctime(filePath)
now = time.time()
fivemin_ago = now - 300
if fileCreation < fivemin_ago:
sys.exit "data was older than five minutes"
bash
NOW=$(date +%s)
MODIFIED=$(date -d "$(stat FILEPATH | grep Modify | awk '{print $2" "$3}')" +%s)
if [ $NOW -gt $[$MODIFIED+300] ];then
echo "data was older than five minutes"
exit 1
else
process_two.py
fi
The best way will make a third file where you can call the first script and get the return after the return you run or not the second script, it will be something like.
#!/usr/bin/env bash
variable=$(download "something")
if [-f "../path/your/file.pdf" ]; then
run here your second command // if file exists run it
fi

How to let loop only stop when statement is true

i wish to run a bash script, that will only stop when the statement is true.
My bash script code is below. I am comparing a date, if compare date is false, script should not stop, keep running till it reach the correct time.
I'm meeting a problem, i wish the script is like this " current time is 03180932015 , if the time reach 031809402015, it will echo to a file, and stop."
But now it's not. If the statement is false it will end the script.
time=`date '+%m%d%H%M%Y'`
if [ $time == "031809402015" ]; then
#echo "hellodd:" > ./aaa.txt
ipmitool -I lanplus -H 192.168.2.2 -U USERID -P PASSW0RD sel list > xxx.txt
fi
done
Try this:
until [[ "$(date '+%m%d%H%M%Y')" == "031809402015" ]]; do
sleep 1
done
ipmitool ...
Try this:
The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.
until command
do
Statement(s) to be executed until command is true
done
Here Shell command is evaluated. If the resulting value is false, given statement(s) are executed. If command istrue then no statement would be not executed and program would jump to the next line after done statement.

Shell script: fire a command if system time is equal to given time

I am very new to shell scripting.
The script should fire a command if the given time as command line argument is equal to the system time, else it should wait(poll) for the given time.
I started with very basics, but i am stuck here only:-(
#!/bin/sh
now=$(date +%k%M)
cur="055" # should be given as command line arg
if ($now == $cur)
then
echo "Fire command here"
else
echo "poll for time"
fi
When i execute the script:
./script.sh: line 5: 055: command not found
poll for time
Thanks for the help.
I think the above is just a small syntax error, instead of:
if ($now == $cur)
You may want to do this:
if [ $now -eq $cur ] #very basic comparison here, you may want to have a
#look at the bash comparisons
Update
Could you change the variable to,
$cur=$(date +%H%M)
And in case the input is not provided by you, you should remove the space in front
of the $now
now=$(echo $now | sed 's/\s//g') #this removes the spaces in the input
You can run a program # a particular time with :
crontab -e
and
0 14 * * * command
to run command # 14 PM (by example)
begin="`date '+%s'`"
final=... #should be converted to seconds since epoch...
sleep $((final - begin))
exec_your_command
The problem you described seems to be exactly what crontab is designed to handle, from wikipedia "Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule."
Here is a quick reference, it is reletively barebones, but should be enough to determine if it meets your needs.
Use following code
if [ $now == $cur ]

How to run script continously in background without using crontab

I have a small script that checks certain condition continously and as soon as that condition is met the program should execute. Can this be done. I thought of using crontab where script runs every 5 min but now I want that to be done without crontab
You probably want to create an infinite loop first, then within that loop you probably want to verify your condition or wait a bit. As you did not mention which scripting language you wanted to use, I'm going to write pseudo code for the example. Give us more info about the scripting language, and perhaps also the conditions.
Example in pseudo code:
# Defining a timeout of 1 sec, so checking the condition every second
timeout = 1
# Running in background infinitely
while true do
# Let's check the condition
if condition then
# I got work to do
...
else
# Let's wait a specified timeout, not to block the system
sleep timeout
endif
endwhile
Example in sh with your input code
#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin
# Defining a timeout of 1 hour
timeout=3600
while true
do
case 'df /tmp' in
" "[5-9]?%" ") rm -f /tmp/af.*
;;
*)
sleep $timeout
;;
esac
done
You can then run this script from the shell using 'nohup':
nohup yourscript &

Resources