最简单的linux服务(service)

  1. 编写一个HelloWorld程序
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc,char **argv)
{
  while(1)
  {
    printf("hello world
");
    sleep(2);//2s
  }
}

  1. gcc编译生成可执行程序:hello
gcc -o hello hello.c
  1. 在/usr/lib/systemd/system目录下创建文件:hello.service,内容如下:
[Unit]
Description=hello service !

[Service]
ExecStart=/home/yan/projects/hello

[Install]
WantedBy=multi-user.target

  1. 运行服务
service hello start
service hello status
service hello stop
原文地址:https://www.cnblogs.com/ziwuxian/p/14899279.html