Debian/Ubuntu应用程序开机自启动

环境:Debian系统或者Ubuntu系统

目的:每次上电后,应用程序自动运行。

 

操作步骤:

1. 编写源代码hello.c。

#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    while(1)
    {
        printf("hello world\n");
        sleep(1);
    }
    return 0;
}

 

2. 编译,路径位于/home/fa/

gcc hello.c -o hello

 

3. 修改开机自启动文件,在“exit 0"前添加一句:"/home/fa/hello”。

sudo vim /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

iptables -t nat -A POSTROUTING -s 192.168.8.0/24 ! -d 192.168.8.0/24  -j MASQUERADE

#在这里添加:/home/fa/hello
/home/fa/hello
exit
0

 

4. 重启即可自动运行hello程序。

sudo reboot

 

5. 查看hello程序是否在运行。

ps -aux | grep hello

  看到这一句说明hello运行正常。

root       464  0.0  0.0   1212   312 ?        S    22:06   0:00 home/fa/hello
posted @ 2017-09-14 22:12  薛文旺  阅读(1456)  评论(0)    收藏  举报