如何在启动时运行shell脚本

时间:2021-06-11 07:08:36

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.

在一个amazon linux实例上,我有两个脚本,分别是start_my_app和stop_my_app,它们可以永远地启动和停止(反过来运行我的节点)。js应用)。我使用这些脚本手动启动和停止我的节点应用。

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.

我的问题是:我还想设置start_my_app在系统启动时运行。我知道我需要在init中添加一个文件。d和我知道如何将它与rc中的适当目录进行符号链接。d,但是不能算出我放入的文件里面到底需要什么。我认为它应该只有一行,比如start_my_app,但这对我来说并不适用。

15 个解决方案

#1


229  

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

在文件中输入/etc/ initit。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

#2


175  

Set a crontab for this

为此设置一个crontab

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

after every startup it will run the test script.

每次启动之后,它都会运行测试脚本。

#3


113  

A simple approach is to add a line in /etc/rc.local :

一个简单的方法是在/etc/rc中添加一行的地方:

/PATH/TO/MY_APP &

or if you want to run the command as root :

或者如果您想以root用户身份运行命令:

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

(the trailing ampersand backgrounds the process and allows the rc.local to continue executing)

(末尾的&号作为进程的背景,允许rc。当地继续执行)

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.

并对它进行一些调整。

#4


26  

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.

把你的脚本放到/etc/ initit中。d,由root和可执行文件所有。在脚本的顶部,您可以为chkconfig提供一个指令。例如,下面的脚本用于作为用户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,

然后,作为用户根,您可以使用chkconfig在启动时启用或禁用脚本,

chkconfig --list apex
chkconfig --add apex

and you can use service start/stop apex

您可以使用服务开始/停止顶点

#5


10  

Another option is to have an @reboot command in your crontab.

另一个选项是在crontab中使用@ restart命令。

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


7  

Just have a line added to your crontab..

只要在crontab中添加一行就可以了。

Make sure the file is executable:

确保文件是可执行的:

chmod +x /path_to_you_file/your_file

To edit crontab file:

编辑crontab文件:

crontab -e

Line you have to add:

你必须补充:

@reboot  /path_to_you_file/your_file

That simple!

这么简单!

#7


5  

Enter cron using sudo:

输入使用sudo cron:

sudo crontab -e

sudo crontab - e

Add a command to run upon start up, in this case a script:

添加一个命令在启动时运行,在本例中是一个脚本:

@reboot sh /home/user/test.sh

@reboot sh /home/user/test.sh

Save:

拯救策略:

Press ESC then :x to save and exit, or hit ESC then ZZ (that's shift+zz)

然后按ESC:x保存和退出,或者按ESC然后按ZZ(即shift+ ZZ)

Test Test Test:

测试测试测试:

  1. Run your test script without cron to make sure it actually works.

    在没有cron的情况下运行测试脚本,以确保它确实有效。

  2. Make sure you saved your command in cron, use sudo crontab -e

    确保您将命令保存在cron中,使用sudo crontab -e

  3. Reboot the server to confirm it all works sudo @reboot

    重新启动服务器以确认它全部工作sudo @reboot

#8


4  

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


1  

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

关于Debian 9,请参见https://askubuntu.com/questions/228304/how-do-i-run-a-script- startup。它帮助了我。Debian 9的短版本:将命令(作为根)添加到/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).

也许,path_to_file /文件名。sh应该是可执行的(我想是的)。

#10


1  

Create your own /init executable

创建自己的/init可执行文件

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:

只需选择任意可执行文件,甚至shell脚本,并使用命令行参数引导内核:

init=/path/to/myinit

Towards the end of boot, the Linux kernel runs the first userspace executable at the given path.

在引导快结束时,Linux内核在给定的路径上运行第一个用户空间可执行文件。

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.

一些项目提供了主要发行版(如systemd)使用的流行的init可执行文件,并且在大多数发行版中,init将为正常的系统操作提供一系列进程。

But we can hijack /init it to run our own minimal scripts to better understand our system.

但是我们可以劫持/init它来运行我们自己的最小脚本,以更好地理解我们的系统。

Here is a minimal reproducible setup: https://github.com/**/linux-kernel-module-cheat/tree/f96d4d55c9caa7c0862991025e1291c48c33e3d9/README.md#custom-init

这里有一个最小可重复的设置:https://github.com/**/linux- kernelel -module- cheat/tree/f96d4d55c9ca7c0862992991025e1291c48e3d9 / readme .md定制初始化

#11


0  

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.

如果您想要运行的所有内容都是一个简单的脚本,那么最简单的方法就是如果您有一个gui来使用系统>首选项,然后启动应用程序。

just browse to the script you want and there you go. (make script executable)

只要浏览到你想要的脚本就可以了。(使脚本可执行)

#12


0  

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.如何在启动时运行shell脚本. Last line is the name of my script.

这个简单的解决方案适用于运行CentOS的Amazon Linux实例。编辑你的/etc/rc.d/rc.本地文件并将命令放在那里。这个文件中提到,它将在所有其他初始化脚本之后执行。所以在这方面要小心。这就是当前文件寻找我的方式。最后一行是我的脚本的名称。

#13


0  

  • Add your script to /etc/init.d/ directory
  • 将脚本添加到/etc/ initit。d /目录
  • 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.
  • 更新rc运行级别:$ Update -rc。d myScript。sh默认为NN,其中NN是应该执行的顺序。例如,它将在98岁之后和100岁之前运行。

#14


-5  

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


-18  

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/ initit。然后是chmod 777 /etc/ initit。d /脚本名称

after apply following command

使用以下命令后

update-rc.d your script defaults remove update-rc.d -f your script remove

update-rc。你的脚本默认删除更新-rc。如果你的脚本被删除

at the startup you can see your app will run.

在启动时,你可以看到你的应用程序会运行。

#1


229  

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

在文件中输入/etc/ initit。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

#2


175  

Set a crontab for this

为此设置一个crontab

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

after every startup it will run the test script.

每次启动之后,它都会运行测试脚本。

#3


113  

A simple approach is to add a line in /etc/rc.local :

一个简单的方法是在/etc/rc中添加一行的地方:

/PATH/TO/MY_APP &

or if you want to run the command as root :

或者如果您想以root用户身份运行命令:

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

(the trailing ampersand backgrounds the process and allows the rc.local to continue executing)

(末尾的&号作为进程的背景,允许rc。当地继续执行)

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.

并对它进行一些调整。

#4


26  

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.

把你的脚本放到/etc/ initit中。d,由root和可执行文件所有。在脚本的顶部,您可以为chkconfig提供一个指令。例如,下面的脚本用于作为用户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,

然后,作为用户根,您可以使用chkconfig在启动时启用或禁用脚本,

chkconfig --list apex
chkconfig --add apex

and you can use service start/stop apex

您可以使用服务开始/停止顶点

#5


10  

Another option is to have an @reboot command in your crontab.

另一个选项是在crontab中使用@ restart命令。

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


7  

Just have a line added to your crontab..

只要在crontab中添加一行就可以了。

Make sure the file is executable:

确保文件是可执行的:

chmod +x /path_to_you_file/your_file

To edit crontab file:

编辑crontab文件:

crontab -e

Line you have to add:

你必须补充:

@reboot  /path_to_you_file/your_file

That simple!

这么简单!

#7


5  

Enter cron using sudo:

输入使用sudo cron:

sudo crontab -e

sudo crontab - e

Add a command to run upon start up, in this case a script:

添加一个命令在启动时运行,在本例中是一个脚本:

@reboot sh /home/user/test.sh

@reboot sh /home/user/test.sh

Save:

拯救策略:

Press ESC then :x to save and exit, or hit ESC then ZZ (that's shift+zz)

然后按ESC:x保存和退出,或者按ESC然后按ZZ(即shift+ ZZ)

Test Test Test:

测试测试测试:

  1. Run your test script without cron to make sure it actually works.

    在没有cron的情况下运行测试脚本,以确保它确实有效。

  2. Make sure you saved your command in cron, use sudo crontab -e

    确保您将命令保存在cron中,使用sudo crontab -e

  3. Reboot the server to confirm it all works sudo @reboot

    重新启动服务器以确认它全部工作sudo @reboot

#8


4  

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


1  

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

关于Debian 9,请参见https://askubuntu.com/questions/228304/how-do-i-run-a-script- startup。它帮助了我。Debian 9的短版本:将命令(作为根)添加到/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).

也许,path_to_file /文件名。sh应该是可执行的(我想是的)。

#10


1  

Create your own /init executable

创建自己的/init可执行文件

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:

只需选择任意可执行文件,甚至shell脚本,并使用命令行参数引导内核:

init=/path/to/myinit

Towards the end of boot, the Linux kernel runs the first userspace executable at the given path.

在引导快结束时,Linux内核在给定的路径上运行第一个用户空间可执行文件。

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.

一些项目提供了主要发行版(如systemd)使用的流行的init可执行文件,并且在大多数发行版中,init将为正常的系统操作提供一系列进程。

But we can hijack /init it to run our own minimal scripts to better understand our system.

但是我们可以劫持/init它来运行我们自己的最小脚本,以更好地理解我们的系统。

Here is a minimal reproducible setup: https://github.com/**/linux-kernel-module-cheat/tree/f96d4d55c9caa7c0862991025e1291c48c33e3d9/README.md#custom-init

这里有一个最小可重复的设置:https://github.com/**/linux- kernelel -module- cheat/tree/f96d4d55c9ca7c0862992991025e1291c48e3d9 / readme .md定制初始化

#11


0  

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.

如果您想要运行的所有内容都是一个简单的脚本,那么最简单的方法就是如果您有一个gui来使用系统>首选项,然后启动应用程序。

just browse to the script you want and there you go. (make script executable)

只要浏览到你想要的脚本就可以了。(使脚本可执行)

#12


0  

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.如何在启动时运行shell脚本. Last line is the name of my script.

这个简单的解决方案适用于运行CentOS的Amazon Linux实例。编辑你的/etc/rc.d/rc.本地文件并将命令放在那里。这个文件中提到,它将在所有其他初始化脚本之后执行。所以在这方面要小心。这就是当前文件寻找我的方式。最后一行是我的脚本的名称。

#13


0  

  • Add your script to /etc/init.d/ directory
  • 将脚本添加到/etc/ initit。d /目录
  • 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.
  • 更新rc运行级别:$ Update -rc。d myScript。sh默认为NN,其中NN是应该执行的顺序。例如,它将在98岁之后和100岁之前运行。

#14


-5  

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


-18  

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/ initit。然后是chmod 777 /etc/ initit。d /脚本名称

after apply following command

使用以下命令后

update-rc.d your script defaults remove update-rc.d -f your script remove

update-rc。你的脚本默认删除更新-rc。如果你的脚本被删除

at the startup you can see your app will run.

在启动时,你可以看到你的应用程序会运行。