Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a shell script at startup

On an Amazon S3 Linux instance, I have two scripts called start_my_app and stop_my_app which start and stop forever (which in turn runs my Node.js application). I use these scripts to manually start and stop my Node.js application. So far so good.

My problem: I also want to set it up such that start_my_app is run whenever the system boots up. I know that I need to add a file inside init.d and I know how to symlink it to the proper directory within rc.d, but I can't figure out what actually needs to go inside the file that I place in init.d. I'm thinking it should be just one line, like, start_my_app, but that hasn't been working for me.

like image 866
meetamit Avatar asked Oct 19 '12 11:10

meetamit


People also ask

How do I run a script on startup Ubuntu?

The Ubuntu 20.04 is based on Systemd hence the simplest and recommended way to run a script on startup is to create a Systemd service file and execute any script such as bash, python etc, via this service during the system boot.

How do I run Ubuntu from terminal at startup?

Open Startup Applications via the Activities overview. Alternatively you can press Alt + F2 and run the gnome-session-properties command. Click Add and enter the command to be executed at login (name and comment are optional).


2 Answers

Set a crontab for this

#crontab -e @reboot  /home/user/test.sh 

after every startup it will run the test script.

like image 50
Hemant kumar Avatar answered Oct 14 '22 04:10

Hemant kumar


In the file you put in /etc/init.d/ you have to set it executable with:

chmod +x /etc/init.d/start_my_app 

Thanks to @meetamit, if this does not run you have to create a symlink to /etc/rc.d/

ln -s /etc/init.d/start_my_app /etc/rc.d/ 

Please note that on latest Debian, this will not work as your script have to be LSB compliant (provide, at least, the following actions: start, stop, restart, force-reload, and status): https://wiki.debian.org/LSBInitScripts

As a note, you should put the absolute path of your script instead of a relative one, it may solves unexpected issues:

/var/myscripts/start_my_app 

And don't forget to add on top of that file:

#!/bin/sh 
like image 44
Jonathan Muller Avatar answered Oct 14 '22 05:10

Jonathan Muller