Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

The script is supposed to run only once, ever. I though to create a file when the script runs the first time and if that file exists, the script will not run again.

What's the best, most clean place, to autostart the script so it runs only once?

share|improve this question
1  
You need to clarify what you mean by "only once, ever". If you really want it to only run one time, ever, just run it manually. –  Marios Zindilis Jul 9 '12 at 18:50
    
Marie I quess he is referring at a script which will run once during a session.Like a script that would run once by the time he logs in. –  Nickolas Jul 9 '12 at 18:59
    
you may want to try CRON JOB...here is the wiki for cron job...there is an option to run a job at startup if thats what you want...or even monthly/yearly or any frequency you want... –  Nirmik Jul 9 '12 at 19:13
    
Only once ever = only one time until the run file gets deleted manually. –  James Mitch Jul 9 '12 at 21:24
    
I can't do it manually, because the script is part of a build script. –  James Mitch Jul 9 '12 at 21:24

1 Answer 1

up vote 2 down vote accepted

I found your question interesting so I tried to figure out how to do it.

I thought this: create a script that could delete itself after being run. This way the script could do its job and then ... goodbye!

Here's the code

#!/bin/bash


# Test script based on informations found on 
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
# trying to understand if it's possibile to achieve what asked on
# http://askubuntu.com/questions/161682/how-to-run-a-script-only-a-single-time-ever

# Let's try to find in which directory the script is
# It should account for spaces, link, etc.
SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
do 
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
  DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# We know the name of the script from 'basename $0'
NAME=`basename $0`

# Merge them to have the full path to the script
FULL_PATH=$DIR/$NAME
echo $FULL_PATH

# Let's be a bit theatrical
echo "This script will self destruct in"
for ((i=5;i>0;i--))
do
    echo $i
    sleep 1
done

# Goodbye!
rm $FULL_PATH

Save this test code and make it executable to test it.
Remember it deletes itself when it's run so don't forget to have a backup before testing it!

share|improve this answer
    
That's clear. The script creates a file and if that file exists, don't run the script again. –  James Mitch Jul 14 '12 at 16:01
    
But the question was: "Where to add this script so it autostarts?". echo "..." >> /etc/rc.local does not work well because the last line is already an exit 0. Where to add it? –  James Mitch Jul 14 '12 at 16:02
    
@JamesMitch It does NOT create a file and checks for its existence, this script deletes the file of the script itself. Don't know about autostart, that should be another question –  Dariopnc Sep 25 '12 at 10:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.