Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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. – Rrj Jul 9 '12 at 18:59
you may want to try CRON 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
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


# Test script based on informations found on 
# trying to understand if it's possibile to achieve what asked on

# Let's try to find in which directory the script is
# It should account for spaces, link, etc.
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
  DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )"
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

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

# Goodbye!

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


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.