Something that I should have learned earlier is a method to revert changes after a certain time in case I lose access to a Linux server. From experience, I can tell you that it's really easy to mess up a client firewall or VPN tunnel.

In short: you’ve been working on a firewall concept and want to activate said firewall and you are not sure if it will cut your connection. The fail-safe method will revert the change if not disabled after n time to allow you to connect again.

More or less a digital dead-man-switch. Be creative!

Creating the fail-safe #

sudo bash -c 'nohup sh -c "sleep 180 && ufw disable" >/dev/null 2>&1 &'

This commands will run ufw disable as sudo 3 minutes after running it. nohup let it run in the background.

There are some methods with screen or at but that methods keeps to work anywhere so far (homogene Ubuntu env).


Removing the fail-safe #

If everything is still working, you can remove it again to prevent it from reverting your changes.

# Lookup the PID of the background process

ps aux | grep ufw

root      585183  0.0  0.0   2888  1980 ?        S    17:21   0:00 sh -c sleep 120 && ufw disable

# Kill background process

sudo kill 585183

# DONE


Running multiple commands #

sudo bash -c 'nohup sh -c "
  sleep 180 && {
    mv backup.conf prod.conf;
    systemctl reload nginx;
    systemctl start n-service;
  }
" >/dev/null 2>&1 &'


Feedback or alternatives are welcome! Enjoy