可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
On an amazon 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 app). I use these scripts to manually start and stop my node app. 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 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.
回答1:
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
回答2:
Set a crontab for this
#crontab -e
@reboot /home/user/test.sh
after every startup it will run the test script.
回答3:
A simple approach is to add a line in /etc/rc.local
:
/PATH/TO/MY_APP &
or if you want to run the command as root :
su - USER_FOOBAR -c /PATH/TO/MY_APP &
(the trailing ampersand backgrounds the process and allows the rc.local to continue executing)
If you want a full init script, debian distro have a template file, so :
cp /etc/init.d/skeleton /etc/init.d/your_app
and adapt it a bit.
回答4:
This is the way I do it on red-hat systems
Put your script in /etc/init.d
, owned by root and executable. At the top of the script, you can give a directive for chkconfig
. Example, the following script is used to start a java application as user oracle.
The name of the script is /etc/init.d/apex
#!/bin/bash
# chkconfig: 345 99 10
# description: auto start apex listener
#
case \"$1\" in
\'start\')
su - oracle -c \"cd /opt/apex ; java -jar apex.war > logs/apex.log 2>logs/apex_error.log &\";;
\'stop\')
echo \"put something to shutdown or kill the process here\";;
esac
this says that the script must run at levels 3, 4 and 5 and the priority for start/stop is 99 and 10.
then, as user root you can use chkconfig
to enable or disable the script at startup,
chkconfig --list apex
chkconfig --add apex
and you can use service start/stop apex
回答5:
Another option is to have an @reboot command in your crontab.
Not every version of cron supports this, but if your instance is based on the Amazon Linux AMI then it will work.
回答6:
Just have a line added to your crontab..
Make sure the file is executable:
chmod +x /path_to_you_file/your_file
To edit crontab file:
crontab -e
Line you have to add:
@reboot /path_to_you_file/your_file
That simple!
回答7:
Enter cron
using sudo
:
sudo crontab -e
Add a command to run upon start up, in this case a script:
@reboot sh /home/user/test.sh
Save:
Press ESC then :x to save and exit, or hit ESC then ZZ (that\'s shift+zz)
Test Test Test:
Run your test script without cron to make sure it actually works.
Make sure you saved your command in cron, use sudo crontab -e
Reboot the server to confirm it all works sudo @reboot
回答8:
You can do it :
chmod +x PATH_TO_YOUR_SCRIPT/start_my_app
then use this command
update-rc.d start_my_app defaults 100
Please see this page on Cyberciti.
回答9:
Create your own /init executable
This is not what you want, but it is fun!
Just pick an arbitrary executable file, even a shell script, and boot the kernel with the command line parameter:
init=/path/to/myinit
Towards the end of boot, the Linux kernel runs the first userspace executable at the given path.
Several projects provide popular init
executables used by major distros, e.g. systemd, and in most distros init will fork a bunch of processes used in normal system operation.
But we can hijack /init
it to run our own minimal scripts to better understand our system.
Here is a minimal reproducible setup: https://github.com/cirosantilli/linux-kernel-module-cheat/tree/f96d4d55c9caa7c0862991025e1291c48c33e3d9/README.md#custom-init
回答10:
This simple solution worked for me on an Amazon Linux instance running CentOS.
Edit your /etc/rc.d/rc.local
file and put the command there. It is mentioned in this file that it will be executed after all other init scripts. So be careful in that regards. This is how the file looks for me currently.. Last line is the name of my script.
回答11:
For Debian 9 see https://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up. It is helped me. Short version for Debian 9:
add commands (as root) to /etc/rc.local
/path_to_file/filename.sh || exit 1 # Added by me
exit 0
Probably, /path_to_file/filename.sh should be executable (I think so).
回答12:
The absolute easiest method if all you want to run is a simple script, (or anything) is if you have a gui to use system > preferences then startup apps.
just browse to the script you want and there you go. (make script executable)
回答13:
- Add your script to /etc/init.d/ directory
- Update your rc run-levels:
$ update-rc.d myScript.sh defaults NN
where NN is the order in which it should be executed. 99 for example will mean it would be run after 98 and before 100.
回答14:
for some people this will works
You could simply add the following command into System > Preferences > Startup Applications:
bash /full/path/to/your/script.sh
回答15:
if you want to put startup also you can use
first of all move your script /etc/init.d then
chmod 777 /etc/init.d/your script name
after apply following command
update-rc.d your script defaults
remove
update-rc.d -f your script remove
at the startup you can see your app will run.