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
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
you have to write
3. On top of this file(auto_run.sh) write
4. Run the script, by writing following on terminal
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"]
Screenshot of auto_run.sh[/caption]
2. Editing rc.local
1. Open rc.local file in root mode.
(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.
3. In end write
4. Check first line to be
5. Run following commands to make rc.local executable
6. Check everything works fine by
7. Restart the system.
[caption id="attachment_118" align="alignnone" width="1024"]
Screenshot of auto_run.sh[/caption]
Additional
Find the process id of a running process
The value in 2nd column is usually the pid.
To kill the process
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.js2. 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.jsyou have to write
node /home/sanyam/Documents/Git/NodeDemo/index.js3. On top of this file(auto_run.sh) write
#!/bin/sh4. Run the script, by writing following on terminal
sh ./auto_run.sh5. 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"]
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.sh3. In end write
exit 04. Check first line to be
#!/bin/sh -e5. Run following commands to make rc.local executable
sudo chown root /etc/rc.localsudo chmod 755 /etc/rc.local6. Check everything works fine by
sudo /etc/init.d/rc.local start7. Restart the system.
[caption id="attachment_118" align="alignnone" width="1024"]
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>
I truly enjoy reviewing your blog post Make program auto-start on Boot in Linux –
ReplyDeleteThe Idea Machine.