Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Linux中编写一个程序,每当我打开电脑时,它都会自动运行?_Linux - Fatal编程技术网

如何在Linux中编写一个程序,每当我打开电脑时,它都会自动运行?

如何在Linux中编写一个程序,每当我打开电脑时,它都会自动运行?,linux,Linux,我每次都在创建一个进程,但在“kill-9-1”之后,我丢失了我创建的进程。我知道为什么每次我都把它弄丢了。。 但是,有没有这样的方法,每次我打开电脑时,我都可以让我的程序自动运行 谢谢,,大多数发行版仍然支持 最简单的方法是从/etc/init.d/中获取一个简单的init脚本,并对其进行更改以满足您的需要: sudo cp /etc/init.d/foo /etc/init.d/my_foo sudo gedit /etc/init.d/my_foo 然后,您需要启用它: sudo /sb

我每次都在创建一个进程,但在“kill-9-1”之后,我丢失了我创建的进程。我知道为什么每次我都把它弄丢了。。 但是,有没有这样的方法,每次我打开电脑时,我都可以让我的程序自动运行


谢谢,,

大多数发行版仍然支持

最简单的方法是从
/etc/init.d/
中获取一个简单的init脚本,并对其进行更改以满足您的需要:

sudo cp /etc/init.d/foo /etc/init.d/my_foo
sudo gedit /etc/init.d/my_foo
然后,您需要启用它:

sudo /sbin/chkconfig my_foo on

如果
chkconfig
不可用,您可能需要安装它。此外,还有一些LSB别名,如
insserv
,可能是可用的。

Ubuntu系统现在附带了,其配置文件可能比SystemV
init
脚本的详细程度要低一些。Upstart的简单作业配置如下所示,例如,
/etc/init/example.conf

# this is a comment
start on startup
stop on shutdown
exec /path/to/program --some-args maybe-another-arg
然后它将分别在启动和关闭时启动和停止。要手动启动和停止它,请使用
start
stop
命令作为
root

$ sudo start example
$ sudo stop example
您可以在中找到有关Upstart配置的更多信息。有关安装Upstart的系统的信息,请参见第5节的
init
手册页。(
man5init

可以让您做到这一点,还可以提供一些其他功能,如FastCGI支持,在服务崩溃时自动重新启动服务(同时保持智能,在服务不断崩溃时不重新启动服务),并保留其输出日志

在和之后,您可以修改其配置文件以添加运行程序的节。一个简单的例子可能是这样的:

; this is a comment
[program:example]
command = /path/to/program --some-args maybe-another-arg
对于一个简单的程序来说,这确实是所有必要的,但还有许多其他配置选项可用

添加配置后,您可以告诉主管添加/删除(以及启动/停止)您在配置中添加或删除的任何进程:

$ sudo supervisorctl update
如果愿意,您还可以手动启动和停止服务:

$ sudo supervisorctl start example
$ sudo supervisorctl stop example
$ sudo supervisorctl restart example
您还可以看到所有流程的漂亮状态显示,例如:

$ sudo supervisorctl status
cgi-pass                         RUNNING    pid 4223, uptime 68 days, 23:57:22
还可以查看程序输出的记录内容:

$ sudo supervisorctl tail example         # stdout
$ sudo supervisorctl tail example stderr  # stderr
$ sudo supervisorctl tail -f example      # continuous

可用命令的文档可通过
supervisorctl help

获得,Fedora随附,许多其他Linux发行版都采用了它(除了Ubuntu和现在的Debian)。该软件包包括几个您可能想查看的帮助程序。

没有足够的信息给出好的答案。可能为您的计算机加载Linux的GRUB或GRUB2引导加载程序可以做很多事情,但您可能需要一个具有用户(您)权限的程序,这建议了一种基于登录时间的方法。这可能是你的问题的错误论坛。这不是更好的提问吗?