3
  • 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
4
  • Are you starting up a server each day, and sometimes more than once a day? I'm having difficulty seeing why. Commented Feb 4, 2010 at 8:31
  • @ Charles Stewart , i asked this question for local server ( LAN )
    – Kumar
    Commented Feb 4, 2010 at 9:29
  • Does the whole LAN really get powered up and down once or more a day? Commented Feb 4, 2010 at 9:38
  • @ Charles Stewart, We save power,
    – Kumar
    Commented Feb 5, 2010 at 4:24

5 Answers 5

3

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.

1

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"
0

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.

0

On a debian system I would put my script on /etc/cron.daily and just install anacron.

Anacron is probably the tool you are looking for.

Don't know if RHEL includes rpm for or if you'll have to hunt for them or even compile from source.

0

You should use anacron to run this. It's used for things that you want once per day.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .