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程序是否在运行, 看到hello程序正在正常运行。
ps -aux | grep hello
root 464 0.0 0.0 1212 312 ? S 22:06 0:00 home/fa/hello