The idea is to initialize a variable and set it to 0. Then create a script that executes on boot and increments that variable by 1 each time the system boots up again.

Sounds simple enough right?

  • (( $(last reboot | wc -l) + 1))? – jordanm Feb 7 '14 at 4:40
  • Too many linux systems rotate wtmp, so that's not going to work. – Ricky Beam Feb 7 '14 at 4:56

Save the variable to a file, and then read that file every time the system boots.

VAR=`cat ~/.foo`
echo $(($VAR + 1 )) > ~/.foo

Then edit ~/.foo and enter in 0

You will need to put this inside a script which is called each boot. Every distro is different.

Some use



  • "on boot" And .bashrc will run on every shell invocation. – Ricky Beam Feb 7 '14 at 5:01
  • @RickyBeam I realized that the moments ago and updated the answer. Thanks for pointing that out. – spuder Feb 7 '14 at 5:02
  • 1
    Some cron implementations like the one from the ISC also support running things at boot (upon the cron daemon starting), which allows non-administrators to run things at boot. (@reboot in the crontab). – Stéphane Chazelas Feb 7 '14 at 7:58
touch /var/log/bootcount
echo $[$(</var/log/bootcount)+1] > /var/log/bootcount

Add that to /etc/rc.local (or equiv for your flavor of linux)

(assuming the system sh is bash)

  • 1
    Assuming that sh is a bash shell is wrong and fails in many cases on all sorts of systems. You can expect sh to be a POSIX shell and nothing more. For scripts as simple as this I would highly recommend to stick to POSIX to keep it as portable as possible. – Marco Feb 7 '14 at 7:29
  • @Marco, agreed, and that same code is as easy to write with POSIX sh syntax: echo "$(($(cat /var/log/bootcount) + 1))" > /var/log/bootcount – Stéphane Chazelas Feb 7 '14 at 7:53
  • It's tagged "linux", so bash is a safe bet. A true shell(TM) would not have any such fanciness, so one would have to resort to expr to get there. (try solaris or aix) – Ricky Beam Feb 7 '14 at 17:59

Adding a line such as this to a system's rc.local should do the trick:

$ touch count.txt; CNT=$(<count.txt); echo $(($CNT + 1)) > count.txt

Where you want to keep the count file is up to you, simply move count.txt or specify it using a variable or a full path. The above takes care of creating the file the first time this gets executed, and will continue to increment the number in that file every time rc.local is executed.

NOTE: The use of $(<count.txt) may present an issue on certain implementations of sh, in those situations you may opt to use $(cat count.txt) instead.


$ rm count.txt 
$ touch count.txt; A=$(<count.txt); echo $(($A + 1)) > count.txt
$ cat count.txt 

$ touch count.txt; A=$(<count.txt); echo $(($A + 1)) > count.txt
$ touch count.txt; A=$(<count.txt); echo $(($A + 1)) > count.txt
$ touch count.txt; A=$(<count.txt); echo $(($A + 1)) > count.txt
$ cat count.txt 
  • 1
    $(<file) is not POSIX. – Stéphane Chazelas Feb 7 '14 at 7:55
  • @StephaneChazelas - you're just stating that right?, I didn't see that as a requirement in the Q and it's tagged as Linux so I was assuming standard tools were fair game. – slm Feb 7 '14 at 7:58
  • if you're putting that in a sh file like most startup scripts are, you should use sh syntax. Many sh interpreters on many Linux based systems don't support that syntax. On unix.SE, if you're suggesting something non-standard, you should point it out. $(<file) is not standard either as per the Linux Software Base specification. – Stéphane Chazelas Feb 7 '14 at 8:02
  • @StephaneChazelas fair enough, changing that command to $(cat count.txt) would remove the issue, correct? – slm Feb 7 '14 at 8:06

Here I present a shell function which reads the file and increments a value stored in the file. The value is stored in the variable count. Finally it writes the incremented value back to the file.



update_data_file() {
  if [ -w "$DATAFILE" ]; then
    while IFS='=' read key value; do
      case "$key" in
        'count') count="$value" ;;
    done < "$DATAFILE"
  else if [ ! -d "$DATAFILEDIR" ]; then
    mkdir -p "$DATAFILEDIR"
  fi fi

  count=$(($count + 1))
  echo "count=$count" > "$DATAFILE"


echo "Boot count is $count"

exit 0

You can place the script into /usr/local/bin and add it to /etc/rc.local.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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