设置linux开机后自动运行的应用程序

  根据linux的开机启动流程,在加载内核后执行/sbin/init,然后根据运行级别i启动/etc/rci.d中的各个脚本,结束后启动个人化设定/etc/rc.d/rc.local脚本

    

    应用程序的执行可以在/etc/rc.d/rc.local这个脚本文件中进行设置

    

    在/home/temp/test目录下放置你的程序,这里以一个简单的程序为例,打开一个file文件,往里写一段话

    

    程序代码如下:

    #include

    #include

    #include

    #include

    int main()

    {

     char buf[]="hello,this is a test program!\n";

     int fd;

     int ret;

     fd=open("/home/temp/test/file",O_RDWR|O_NONBLOCK);

    

     ret=write(fd,buf,sizeof(buf));

     return 0;

    }

    

    编译后生成可执行文件test,放在/home/temp/test,然后在该目录下创建一个空文件file,以便验证启动后程序是否被执行

    

    修改/etc/rc.d/rc.local脚本文件

    

    添加这么一段换:

    

    echo "start my test program now.......... "

    /home/temp/test/test

    sleep 3

    echo "test program starts OK"

    

    保存后重启系统,查看启动信息,看是否有上面的话打印出来,启动后到/home/temp/test看file文件里是否有hello,this is a test program!这句话,有则程序启动成功。

你可能感兴趣的:(Linux平台)