- Run shell script at linux boot (Start up) but only one time per day , How can i do it ?
- I am using Redhat enterprise linux 5
-
Are you starting up a server each day, and sometimes more than once a day? I'm having difficulty seeing why.– Charles StewartCommented Feb 4, 2010 at 8:31
-
@ Charles Stewart , i asked this question for local server ( LAN )– KumarCommented Feb 4, 2010 at 9:29
-
Does the whole LAN really get powered up and down once or more a day?– Charles StewartCommented Feb 4, 2010 at 9:38
-
@ Charles Stewart, We save power,– KumarCommented Feb 5, 2010 at 4:24
5 Answers
Put your script in the init.d so that it gets executed at boot.
To make sure that it only gets executed once a day, all you need to do is store the date of the previous execution and compare that to the current date. This is quite simple to do in any script language.
I use this. The code below save to excutable file, name depo, and you read comment in file.
#!/bin/bash
# depo = day-execute-per-onetime = execute onetime per day
# add argumets whose execute onetime per day
# Example: ./depo "uname -a" # let's try run twotimes
if [ $# = 0 ]; then
echo "Missing arguments, add one or more commands what you want to execute. Like $ depo \"uname -a\" date";
exit;
fi;
TODAY=`date +%Y-%m-%d`
HOME_DIR="$HOME/.depo/"
COMMAND=`echo $@ | sha1sum | cut -d ' ' -f1`
SYNC_FILE="$HOME_DIR/$COMMAND"
mkdir -p "$HOME_DIR"
touch "$SYNC_FILE"
SYNC_DATE=`cat "$SYNC_FILE"`
if [ "$SYNC_DATE" == "$TODAY" ]; then
exit
fi
for arg; do
eval $arg
done
echo $TODAY > "$SYNC_FILE"
Add it to init.d. Have the script "touch" a small file to a directory it has access to, a home directory as a non-privileged user (unless it needs other privs). Do not use tmp since you have no guarantees that the file will persist. Name the file with the name of the process, look for it and check the last modification time on it. If its less than 86400 seconds ago, exit, else continue. Do the check prior to "touch"ing the file or you will always think that the script has not run in the last day.
You should use anacron to run this. It's used for things that you want once per day.