Notifications
Clear all

Linux: How to execute a bash script at system startup using cron

1 Posts
1 Users
0 Reactions
2 Views
Tharindu
(@tharindu)
Reputable Member Admin
Joined: 10 years ago
Posts: 309
Topic starter  

There a few ways to run commands on system bootup or reboot on Linux. One way is by using crontab.

I had this problem on Hetzner where server had a delay starting IPV6 network and Nginx try to start before it. So it fails. I had to manually start Nginx after few seconds and it became very annoying. I needed a way to start nginx few seconds after the system start up. Here's how I did it.

First create a bash script,

nano /opt/post-startup.sh

Then paste commands,

#!/bin/bash

#Add your commands below
sleep 20
systemctl nginx start

Make the script executable,

chmod +x /opt/post-startup.sh

Open crontab,

crontab -e

Paste cron command,

@reboot bash /opt/post-startup.sh > /dev/null 2>&1

Save and reboot the server to check.

Now you can include all your commands in post-startup.sh and cron will execute them at system reboot. The sleep commands tells to wait certain amount of time executing the next command. I needed 20 second delay.

 

 


   
Quote
Topic Tags
Share:
Back to top button