1.写一个简单小程序

#include<stdio.h>
#include<stdlib.h> int main(int argc,char **argv)
{
  while(1)
  {
    printf("hello world\n");
    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!

简单的linux service(linux服务)编写,运行示例的更多相关文章

  1. linux中service *** start与直接运行/usr/bin/***的区别

    在linux想要运行启动一个服务有两种方法: 1,运行/etc/init.d/目录下的shell脚本,还可以有快捷方式,service *** start/ stop/restart /status, ...

  2. Linux服务和运行级别科普

    在Linux中,列出所有的系统服务 chkconfig --list 输入以上命令可以看到类似以下的结果 sysstat :关闭 :关闭 :启用 :启用 :关闭 :启用 :关闭 tcsd :关闭 :关 ...

  3. linux中的openoffice服务终止运行

    现象: 最近的linux中的openoffice服务进程运行一段时间后会自动停止,刚开始还以为忘了启动执行自启动脚本导致的.在连续出现前述情况后,开始查找应用程序崩溃的原因,首先查看linux服务器的 ...

  4. netcore一键部署到linux服务器以服务方式后台运行

    @font-face { font-family: octicons-link; src: url("data:font/woff;charset=utf-8;base64,d09GRgAB ...

  5. linux service 简单易懂贴

    service用于管理Linux操作系统中服务的命令 1.不是在所有linux发行版本中都有.主要是在redhat.fedora.mandriva和centos中. 2. 命令位于/sbin目录下,用 ...

  6. [转载] 在Linux中,开机自动运行普通用户的脚本程序

    FROM:http://blog.csdn.net/sinboy/article/details/2466225 FROM:http://www.2cto.com/os/201006/50680.ht ...

  7. Linux之crond 服务介绍

    在介绍crond之前,先科普一下什么是进程?什么是线程?什么是程序? 程序:程序是一组指令及参数的集合,指令按照既定的逻辑控制计算机运行.进程则是运行着的程序,是操作系统执行的基本单位.线程则是为了节 ...

  8. Linux下网络服务的安全设置

    Linux下网络服务的安全设置      Linux操作系统由于其良好的稳定性.健壮性.高效性和安全性.正在成为各种网络服务的理想平台.各种网络应用在Linux系统上部有性能卓越的应用,例如,提供We ...

  9. Linux之systemd服务配置及自动重启

    layout: post title: Linux之systemd服务配置及自动重启 date: 2019-09-09 tags: linux --- Linux之systemd服务配置及自动重启 0 ...

  10. Linux下tomcat服务

    一:Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/loca ...

随机推荐

  1. C++空类大小

    class a {};class b{};class c:public a{ virtual void fun()=0;};class d:public b,public c{}; 类a,b明明是空类 ...

  2. unix下网络编程之I/O复用(一)

    什么是I/O复用? What we need is the capability to tell the kernel that we want to be notified if one or mo ...

  3. 树结构ztree的 ajax交互的简单使用

    今天做前端页面要用到树结构,用了第三方插件ztree,搞了好久不过终于弄出来了,, 一点小心得.(用的版本 V3 ) 首先看下载的文件结构: 一:将要用到的CSS 和 JS 拷贝到工程中,我这里在工程 ...

  4. DCloud-5+Runtime:杂项

    ylbtech-DCloud-5+Runtime:杂项 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部 ...

  5. c# 几种singleton 实现

    http://csharpindepth.com/Articles/General/Singleton.aspx#introduction 4th在线看 https://www.manning.com ...

  6. 找不到引用microsoft.office.core解决办法 via mrcjiong

    在控制面板中,选择"添加删除程序",找到office ,选择"更改",在对话框中选择"添加删除功能",然后选择自定义安装,添加上office ...

  7. Install boot2docker defaule username and password

    username passowrd method docker tcuser ssh root   command sudo -i

  8. [Chapter 3 Process]Practice 3.2 Including the initial parent process, how many processes are created by the program shown in Figure?

    3.2 Including the initial parent process, how many processes are created by the program shown in Fig ...

  9. JVM中存储类信息的三个表格

    摘自:<Java Performance>第三章 Internal Class Loading Data The HotSpot VM maintains three hash table ...

  10. ASP.NET 连接MySql数据库

    ASP.NET Mysql操作类 以下连接MySql数据库以VS2010为例,对于其他的编辑器也差不多 1. 我们需要在Mysql官网下载一个组件http://dev.mysql.com/downlo ...