Make program auto-start on Boot in Linux

Making a program run automatically is very easy. It can be broken down into 2 steps:


1. Write a shell script which contains all the commands(or program name) you want to run when Linux Boots.(Trust me, you will find it super easy, once I tell you :P)
2. Edit /etc/rc.local file.

1. Preparing Shell Script
1. Suppose you have a program called index.js, which you run normally by
node index.js
2. Now make a new text file with sh extension (suppose auto_run.sh).
3. Write the code you run on terminal in the text file(auto_run.sh).
4. Be careful that you write absolute address of the file(index.js).
Instead of
node index.js
you have to write
node /home/sanyam/Documents/Git/NodeDemo/index.js
3. On top of this file(auto_run.sh) write
#!/bin/sh
4. Run the script, by writing following on terminal
sh ./auto_run.sh
5. If everything works as it was working in step 1, script is fine, else check the script(abc_run.sh).

[caption id="attachment_119" align="alignnone" width="1024"]auto_run Screenshot of auto_run.sh[/caption]

2. Editing rc.local
1. Open rc.local file in root mode.
sudo vim /etc/rc.local
(vim is a text editor, you can replace vim by geany)
2. Now write the command to run script(but again with an absolute address) at end of this file.
sh /home/sanyam/Documents/git/NodeDemo/auto_run.sh
3. In end write
exit 0
4. Check first line to be
#!/bin/sh -e
5. Run following commands to make rc.local executable
sudo chown root /etc/rc.local
sudo chmod 755 /etc/rc.local
6. Check everything works fine by
sudo /etc/init.d/rc.local start
7. Restart the system.

[caption id="attachment_118" align="alignnone" width="1024"]rc.local Screenshot of auto_run.sh[/caption]

Additional
Find the process id of a running process
ps aux|grep <process name>
The value in 2nd column is usually the pid.

To kill the process
kill pid <pid>

Comments

  1. I truly enjoy reviewing your blog post Make program auto-start on Boot in Linux –
    The Idea Machine.

    ReplyDelete

Post a Comment