how can I run my own script at every bootup

只谈情不闲聊 提交于 2019-12-12 09:53:44

问题


I have a question, how can I run my own bash script at every startup time in Ubuntu. Suppose I have a script which is doing a particular kind of work. Now I want it to run automatically at the time of starting my Ubuntu system.


回答1:


You should learn how to use upstart. See this.




回答2:


Currently Linux systems (including Ubuntu) support 2 ways of achieving this: Upstart and SysV scripts. Upstart is the "new" way.

Generating SysV scripts can be achieved like so:

update-rc.d <your script> defaults

This will make links to start the service in runlevels 2345 and to stop the service in runlevels 016 and will create the appropriate SysV-style scripts inside /etc/rc?.d/

The other way would be to write an upstart-job. Upstart jobs are located under /etc/init. The easiest way is to copy an existing job and try to modify it for your script. Here's the upstart stanzas explained.




回答3:


There are two options. The easy one, edit /etc/rc.local and call your script from there. The other option is to use upstart. Hace a look at /etc/init/hostname.conf. You can use that file as template, copy it as /etc/init/yourscript.conf, adapt the content and it should work.




回答4:


I have recently run into a situation where one job is ideally started with upstart, and the other one rc.local. Although both methods will get your script executed, upstart makes more sense when daemonizing a script at start time; rc.local, on the other hand, lets the script run its course.

For example, if the script is already a daemon process, e.g. a server, it can be invoked by rc.local, as it doesn't stay in the foreground and block the terminal.

But if the script itself is not daemonized, upstart will give it the ability to gracefully start and stop, upon system events. This is good for running a non-exiting PHP script in the background. Although you could use upstart to run, say, Apache httpd, the process "exits" right away in the eyes of Upstart, which makes Upstart pointless, as it is already "terminated".



来源:https://stackoverflow.com/questions/9871997/how-can-i-run-my-own-script-at-every-bootup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!