I have my raspberry pi set with an autologin feature, however sometimes when a third party program crashes within the raspberry pi it logs me out and deactivates the automatic login. I'm trying to create a background program that detects when I'm logged out and automatically restarts the raspberry pi. is there any way to do this? Any help greatly appreciated.

1 Answer 1


There is a watchdog service for Linux and Raspberry pi OS called Watchdog.

Howto enable Watchdog on Raspberry Pi

Stage 1: Activating watchdog hardware in pi

Open the following file:
sudo nano /boot/config.txt
and at the end of this file add following lines:
# Enabling watchdog.

Reboot your raspberry pi. After reboot list devices with the name prefixed by watchdog, to do so run the following command:

ls -al /dev/watchdog*
It will display output like this:
crw------- 1 root root  10, 130 May 19 07:09 /dev/watchdog
crw------- 1 root root 253,   0 May 19 07:09 /dev/watchdog0

There are two devices that you can see above. If you use one of the watchdog’s, trying to use the other one gives you a permission error. Bottom line, there was and is only one hardware watchdog provided.

Stage 2: Installing watchdog To install watchdog run this command on pi’s terminal

sudo apt-get install watchdog

Once finished this installation will add the necessary systemd files, to verify them run this command.

ls -l /lib/systemd/system/

and you will get the output something like below on your terminal.


The configuration file created for watchdog is this, we will not edit this for now as it looks perfect for now.

ls -al /etc/default/watchdog

# Start watchdog at boot time? 0 or 1


# Start wd_keepalive after stopping watchdog? 0 or 1


# Load module before starting watchdog


# Specify additional watchdog options here (see manpage).

Stage 3: Configuring watchdog to respond to events To add your configuration edit this file

nano /etc/watchdog.conf

Uncomment the following lines or add them in the bottom of the files if you don’t find these lines in the above file.

max-load-1 = 24
min-memory = 1

watchdog-device = /dev/watchdog

There is one more line that I added at the end of the above file is this.


The reason behind it is that it was throwing me below error when I run status check on watchdog :

cannot set timeout 60 (errno = 22 = ‘Invalid argument’)

Stage 4: Starting/Monitoring watchdog service: To start the service:

sudo systemctl start watchdog

To check watchdog status:

sudo systemctl status watchdog

if you get cannot set timeout error please see the fix at the end of the above section. To stop the service use this command:

sudo systemctl stop watchdog

Stage 5: Add watchdog on boot. Edit the following file


and add the following lines under the Install section.


save it and run this command:

sudo systemctl daemon-reload    
sudo systemctl enable watchdog

For your process monitoring option:-

TEST DIRECTORY Executables placed in the test directory are discovered by watchdog on startup and are automatically executed. They are bounded time-wise by the test-timeout directive in watchdog.conf. These executables are called with either "test" as the first argument (if a test is being performed) or "repair" as the first argument (if a repair for a previously-failed "test" operation on is being performed).

The as with test binaries and repair binaries, expected exit codes for a successful test or repair operation is always zero.

If an executable's test operation fails, the same executable is automatically called with the "repair" argument as well as the return code of the previously-failed test operation.

For example, if the following execution returns 42:

/etc/watchdog.d/my-test test

The watchdog daemon will attempt to repair the problem by calling:

/etc/watchdog.d/my-test repair 42

This enables administrators and application developers to make intelligent test/repair commands. If the "repair" operation is not required (or is not likely to succeed), it is important that the author of the command return a non-zero value so the machine will still reboot as expected.

Note that the watchdog daemon may interpret and act upon any of the reserved return codes noted in the Check Binary section prior to calling a given command in "repair" mode.

  • thank you so much for this reply, and this should reboot the pi automatically when any system errors or crashes occur? Apr 28 at 21:19
  • Yes you can add simple test scripts in a watchdog.conf configured test-directory (/etc/watchdog.d) which are run periodically and if they fail it can cause a reboot. Apr 29 at 11:43
  • I've added the relevant man page for the test directory.. Have a look at this page for more detail.. Basically depending on the return value/status of the test script it will inform the watchdog process what todo.. crawford-space.co.uk/old_psc/watchdog/… Apr 29 at 11:46

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.