lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

[os]   xx.service文件编写 -- systemctl

 

 

 

 

一、 基本信息

 

  1、 操作系统:  Linux rocky 5.14.0-427.16.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 8 17:48:14 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

 

 

 


二、 c语言程序可运行文件(systest)

 

 

  1、  源代码(systest.c)

 1 [root@rocky:c]# cat systest.c 
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 
 6 void
 7 msg
 8 ()
 9 {
10     printf("\n\t[lidawei]#\tsystemctl test\n\n");
11 }
12 
13 
14 int
15 main
16 ()
17 {
18     msg();
19 
20     return 0;
21 }

 

 

  2、  文件编译(systest.c)

[root@rocky:c]# gcc -o systest systest.c 
[root@rocky:c]# 
[root@rocky:c]# 
[root@rocky:c]# pwd
/root/rocky93/lidawei/tmp/c
[root@rocky:c]# 
[root@rocky:c]# 
[root@rocky:c]# ls
asm_macro_bugs.c  systest  systest.c  systest.service  test  test-as  test.as  test.c  testcopy  test.i  test.s  upper_lower  upper_lower.c
[root@rocky:c]# 
[root@rocky:c]# 

 

 

 

 

三、 systest.service(position: /usr/lib/systemd/system):

 


  1、 文件的基本信息:

             文件                     文件位置
        1)  systest(elf)            /root/rocky93/lidawei/tmp/c/systest
        2)  systest.service          /usr/lib/systemd/system/systest.service

 

 

   2、  文件编写【 systest.service 】:

[root@rocky:c]# cat  systest.service 
# lidawei config  --  date: 2024-05-22
# file_name = systest.service



[Unit]
Description=systemctl command test 


[Service]
# running part: ExecStart
ExecStart=/root/rocky93/lidawei/tmp/c/systest

[Install]
WantedBy=multi-user.target
[root@rocky:c]# 
[root@rocky:c]# 

 


  3、 systest.service 文件放置: cp    systest.service     /usr/lib/systemd/system/systest.service

 

 

  4、 systemctl enable systest.service 等效命令: ln   -s     /usr/lib/systemd/system/systest.service    /etc/systemd/system/multi-user.target.wants/systest.service

  


  5、 运行 systest.service :    systemctl  start  systest.service

 


  6、 查看 ‘5’ 的运行结果: systemctl  status  systest.service

[root@rocky:c]# systemctl  start systest.service 
[root@rocky:c]# systemctl  status systest.service 
○ systest.service - systemctl command test
     Loaded: loaded (/usr/lib/systemd/system/systest.service; disabled; preset: disabled)
     Active: inactive (dead)

May 22 23:02:59 rocky systemd[1]: Started systemctl command test.
May 22 23:02:59 rocky systest[2575]:         [lidawei]#        systemctl test
May 22 23:02:59 rocky systemd[1]: systest.service: Deactivated successfully.
[root@rocky:c]# 

 



 

四、 参考资料

 

  1、 systemctl(1) command    -   https://cloud.tencent.com/developer/article/2339053

 

posted on 2024-05-22 23:14  lnlidawei  阅读(10)  评论(0编辑  收藏  举报