Detecting restart since last run in a shell script on Linux?

If you don't want to use cron, you could also create a directory in /dev/shm. Since that location is in memory, it will always be empty when the computer starts up. If it's not empty, you haven't rebooted.


  • Create a directory somewhere when the system starts.
  • When your script runs if the directory exists then the system has restarted otherwise not.
  • Get your script to remove the directory.

Use the @reboot directive in /etc/crontab to create the directory when the system starts.


Another approach would be to touch a file somewhere in the startup scripts and search for that in the script, the script could then delete once it's run the first time.

Just be careful you deal with runtime level changes and so forth nicely.


sar command will help you in this.

http://linux.die.net/man/1/sar