如何在启动时运行shell脚本

本文翻译自:How to run a shell script at startup

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). 在亚马逊linux实例上,我有两个名为start_my_appstop_my_app脚本,它们永远启动和停止(后者又运行我的node.js应用程序)。 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. 我的问题:我也想设置它, start_my_app在系统启动时运行start_my_app 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 . 我知道我需要在init.d添加一个文件,我知道如何将它符号链接到rc.d的正确目录,但是无法弄清楚我在init.d放置的文件中究竟需要什么。 。 I'm thinking it should be just one line, like, start_my_app , but that hasn't been working for me. 我认为它应该只是一行,比如, start_my_app ,但这对我start_my_app


#1楼

参考:https://stackoom.com/question/sR4T/如何在启动时运行shell脚本


#2楼

A simple approach is to add a line in /etc/rc.local : 一个简单的方法是在/etc/rc.local添加一行:

/PATH/TO/MY_APP &

or if you want to run the command as a special user : 或者如果要以特殊用户身份运行该命令:

su - USER_FOOBAR -c /PATH/TO/MY_APP &

(the trailing ampersand backgrounds the process and allows the rc.local to continue executing) (尾随&符号表示进程并允许rc.local继续执行)

If you want a full init script, debian distro have a template file, so : 如果你想要一个完整的init脚本,debian发行版有一个模板文件,所以:

cp /etc/init.d/skeleton /etc/init.d/your_app

and adapt it a bit. 并适应它。


#3楼

In the file you put in /etc/init.d/ you have to set it executable with: 在您放入/etc/init.d/的文件中,您必须将其设置为可执行文件:

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/ 感谢@meetamit,如果没有运行,你必须创建一个符号链接到/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 请注意,在最新的Debian上,这将不起作用,因为您的脚本必须符合LSB(至少提供以下操作:启动,停止,重新启动,强制重新加载和状态): 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

#4楼

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 首先移动你的脚本/etc/init.d然后chmod 777 /etc/init.d/你的脚本名称

after apply following command 应用以下命令后

update-rc.d your script defaults remove update-rc.d -f your script remove update-rc.d你的脚本默认删除update-rc.d -f你的脚本删除

at the startup you can see your app will run. 在启动时,您可以看到您的应用程序将运行。


#5楼

Another option is to have an @reboot command in your crontab. 另一个选择是在crontab中有一个@reboot命令。

Not every version of cron supports this, but if your instance is based on the Amazon Linux AMI then it will work. 并非每个版本的cron都支持此功能,但如果您的实例基于Amazon Linux AMI,那么它将起作用。


#6楼

This is the way I do it on red-hat systems 这是我在红帽系统上的方式

Put your script in /etc/init.d , owned by root and executable. 将您的脚本放在/etc/init.d ,由root和可执行文件拥有。 At the top of the script, you can give a directive for chkconfig . 在脚本的顶部,您可以为chkconfig指定一个指令。 Example, the following script is used to start a java application as user oracle. 例如,以下脚本用于以用户oracle启动java应用程序。

The name of the script is /etc/init.d/apex 脚本的名称是/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. 这表示脚本必须在级别3,4和5运行,并且启动/停止的优先级为99和10。

then, as user root you can use chkconfig to enable or disable the script at startup, 然后,作为root用户,您可以使用chkconfig在启动时启用或禁用脚本,

chkconfig --list apex
chkconfig --add apex

and you can use service start/stop apex 你可以使用服务启动/停止顶点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值