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
Kumar
  • 823
  • 3
  • 20
  • 43

5 Answers5

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.

Antoine Benkemoun
  • 7,314
  • 3
  • 41
  • 60
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"
h4kuna
  • 11
  • 1
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.

arunpereira
  • 131
  • 2
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.

scyldinga
  • 178
  • 1
  • 6
0

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

Amandasaurus
  • 30,211
  • 62
  • 184
  • 246