zoukankan      html  css  js  c++  java
  • 简单的linux service(linux服务)编写,运行示例

    1.写一个简单小程序

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

     2.gcc编译

    gcc -o hello hello.c
    

     生成hello

    ./hello
    

     测试,ok!

    3.在/etc/init.d/目录下生成hello.sh脚本

    hello.sh:

    #!/bin/bash
    
    SERVERNAME="hello"
    
    start()
    {
        echo "start $SERVERNAME"
        /home/yao/projects/$SERVERNAME
        echo "start $SERVERNAME ok!"
        exit 0;
    }
    
    stop()
    {
        echo "stop $SERVERNAME"
        killall $SERVERNAME
        echo "stop $SERVERNAME ok!"
    }
    
    case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "usage: $0 start|stop|restart"
        exit 0;
    esac
    exit
    

    4.更改脚本文件属性

    chmod +x hello.sh
    

    5.运行

    (1)启动:

    root@localhost:/home/yao/projects# service hello.sh start
    start hello
    hello world
    

    (2)停止:

    root@localhost:/home/yao/projects# service hello.sh stop
    stop hello
    stop hello ok!
    

    (3)重启:

    root@localhost:/etc/init.d# service hello.sh restart
    stop hello
    stop hello ok!
    start hello
    hello world
    hello world
    

     6.over!

  • 相关阅读:
    Airflow使用笔记
    公共关系学(第二版)笔记
    公众关系秘籍
    SQL SERVER XML 学习总结
    hadoop 1.2.1 配置
    ssh
    centos&windows的共享文件(通过挂载)
    代理设置(wget/yum)
    环境变量设置
    centos7 自定义服务
  • 原文地址:https://www.cnblogs.com/yaosj/p/6601649.html
Copyright © 2011-2022 走看看