linux systemctl service examples
一、脚本服务化目的
1、python 在 文本处理中有着广泛的应用,为了满足文本数据的获取,会每天运行一些爬虫抓取数据。但是网上买的服务器会不定时进行维护,服务器会被重启。这样我们的爬虫服务就无法运行。这个时候我们可以把python脚本服务化,服务器重启后,脚本就会自动运行。解决服务器维护后需要手动运行python脚本。
2、实现方法:
给编写好的python脚本开头加上
#!/usr/bin/python
3、启动shell 脚本 编写
vi pystock.sh
#vim /etc/init.d/httpd
#!bin/bash
lock="py_stock.py" #启动服务方法
start(){
echo "service start...."
su root -c "python /root/python/py_stock/src/crawler/py_stock.py &"
} #停止服务方法
stop(){ echo "service stop...." pkill -f $lock} #查看服务状态
status(){
if [ -e $lock ];then
echo "$0 service start"
else
echo "$0 service stop"
fi
} #重新启动
restart(){
stop
start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
echo "$0 start|stop|status|restart"
;;
esac
3、复制脚本到/etc/init.d/目录下:cp pystock.sh /etc/init.d/
4、给shell脚本赋予执行权限 :chmod +x /etc/init.d/pystock.sh
5、添加服务:chkconfig --add pystock.sh
6、设置服务为开机启动:chkconfig --level 35 pystock.sh on
出现的问题:
当我运行shell 脚本启动python脚本时,提示我一下错误
syntax error near unexpected token `$'{\r''
这是因为window 下换行是\r\n,linux 下换行是\n。我在window下编写的shell 脚本拷贝到linux上。shell命令解读时会先解读/r,在解读后面的脚本导致报错。而且这个\r 在Linux上是看不到的。
解决方法:
sed 's/\r//' 原文件 >转换后文件
二、参考:http://0pointer.de/public/abrtd
#!/bin/bash
# Starts the abrt daemon
#
# chkconfig: 35 82 16
# description: Daemon to detect crashing apps
# processname: abrtd
### BEGIN INIT INFO
# Provides: abrt
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Stop: 0 1 2 6
# Default-Start: 3 5
# Short-Description: start and stop abrt daemon
# Description: Listen to and dispatch crash events
### END INIT INFO # Source function library.
. /etc/rc.d/init.d/functions
ABRT_BIN="/usr/sbin/abrtd"
LOCK="/var/lock/subsys/abrtd"
OLD_LOCK="/var/lock/subsys/abrt"
RETVAL=0 #
# Set these variables if you are behind proxy
#
#export http_proxy=
#export https_proxy= #
# See how we were called.
# check() {
# Check that we're a privileged user
[ "`id -u`" = 0 ] || exit 4 # Check if abrt is executable
test -x $ABRT_BIN || exit 5
} start() { check # Check if it is already running
if [ ! -f $LOCK ] && [ ! -f $OLD_LOCK ]; then
echo -n $"Starting abrt daemon: "
daemon $ABRT_BIN
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $LOCK
echo
fi
return $RETVAL
} stop() { check echo -n $"Stopping abrt daemon: "
killproc $ABRT_BIN
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $LOCK
[ $RETVAL -eq 0 ] && rm -f $OLD_LOCK
echo
return $RETVAL
} restart() {
stop
start
} reload() {
restart
} case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
force-reload)
echo "$0: Unimplemented feature."
RETVAL=3
;;
restart)
restart
;;
condrestart)
if [ -f $LOCK ]; then
restart
fi
# update from older version
if [ -f $OLD_LOCK ]; then
restart
fi
;;
status)
status abrtd
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|force-reload}"
RETVAL=2
esac exit $RETVAL
三、参考系统示例
[root@docker-server ~]# cd /etc/init.d/
[root@docker-server init.d]# ll
total 32
-rw-r--r--. 1 root root 15301 Aug 30 2016 functions
-rwxr-xr-x. 1 root root 2989 Aug 30 2016 netconsole
-rwxr-xr-x. 1 root root 6834 Aug 30 2016 network
-rw-r--r--. 1 root root 1160 Oct 12 2016 README
[root@docker-server init.d]# cvat README
-bash: cvat: command not found
[root@docker-server init.d]# cat README
You are looking for the traditional init scripts in /etc/rc.d/init.d,
and they are gone? Here's an explanation on what's going on: You are running a systemd-based OS where traditional init scripts have
been replaced by native systemd services files. Service files provide
very similar functionality to init scripts. To make use of service
files simply invoke "systemctl", which will output a list of all
currently running services (and other units). Use "systemctl
list-unit-files" to get a listing of all known unit files, including
stopped, disabled and masked ones. Use "systemctl start
foobar.service" and "systemctl stop foobar.service" to start or stop a
service, respectively. For further details, please refer to
systemctl(1). Note that traditional init scripts continue to function on a systemd
system. An init script /etc/rc.d/init.d/foobar is implicitly mapped
into a service unit foobar.service during system initialization. Thank you! Further reading:
man:systemctl(1)
man:systemd(1)
http://0pointer.de/blog/projects/systemd-for-admins-3.html
http://www.freedesktop.org/wiki/Software/systemd/Incompatibilities
四、利用systemctl添加自定义系统服务
参考:https://www.cnblogs.com/saneri/p/7778756.html
linux systemctl service examples的更多相关文章
- 时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell
时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell opensuse 一些常用命令: service xxx start/s ...
- linux systemctl 常用用法简介
主要介绍systemctl的几个功能如下: 1.查看某个服务的状态 2.关闭某个服务 3.开启某个服务 4.设置某个为开机自启动 5.关闭某个服务为开机不启动 6.查看所有开启启动的服务 1.查看某个 ...
- 在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service
在 CentOS7 上将自定义的 jar 包注册为 linux 服务 service 1.在 /etc/rc.d/init.d/ 目录下创建一个名字和服务名完全相同的 shell 脚本文件 joyup ...
- 将Apache加入到linux系统service
将Apache加入到linux系统service 将apache加入到linux系统服务,用service命令来控制apache的启动和停止. 本文由乌合之众瞎写http://www.cnblogs. ...
- Linux: 20 Iptables Examples For New SysAdmins
Linux comes with a host based firewall called Netfilter. According to the official project site: net ...
- Linux: service network/Network/NetworkManager
Linux:service network/Network/NetworkManager start 这三种有什么不同? 1.network service的制御网络接口配置信息改动后,网络服务必须从 ...
- Linux中service命令和/etc/init.d/的关系
Linux中service命令和/etc/init.d/的关系 service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...
- Linux命令service - 系统服务管理(转)
用途说明 service命令用于对系统服务进行管理,比如启动(start).停止(stop).重启(restart).查看状态(status)等.相关的命令还包括chkconfig.ntsysv等,c ...
- Azure的CentOS上安装LIS (Linux Integration Service)
Azure上虚拟化技术都是采用的Hyper-v,每台Linux虚拟机都安装了LIS(Linux Integration Service).LIS的功能是为VM提供各种虚拟设备的驱动.所以LIS直接影响 ...
随机推荐
- python3,循环,方法练习2
1:编写for循环,利用索引遍历出每一个字符 msg = 'hello egon 666' msg = 'hello egon 666' i = 0 for i in range(0, len(msg ...
- UNREFERENCED_PARAMETER()的作用
告诉编译器,已经使用了该变量,不必警告. 在VC编译器下,当你声明了一个变量,而没有使用时,编译器就会报警告: “warning C4100: ''XXXX'' : unreferenced form ...
- selenium与360极速浏览器driver配置
1)下载浏览器对应的driver,浏览器版本与driver对应关系,网址:http://www.cnblogs.com/JHblogs/p/7699951.html:driver下载地址:http:/ ...
- mysql索引语法及示例
注:本篇文章是对菜鸟教程中的mysql索引(http://www.runoob.com/mysql/mysql-index.html)的翻译版本:添加了示例,便于理解: 索引分单列索引和组合索引.单列 ...
- text-overflow的用法
在平时的网页制作中一定碰到过内容溢出的问题,比如说文章列表很长,而其宽度又受到限制,此时超出宽度的内容就会以(...)显示.以前实现这样的效果都是由后台程序截取一定的字符数在前台输出,另外一种方法就是 ...
- javascript中判断变量时变量值为 0 的特殊情况
有时候我们在js中会直接判断变量是否存在值,下面列举一些情况: var a = 0; var b = 1; var c = ' '; var d; console.log( a ? 1 : null) ...
- 《Java编程思想》笔记 第二章 一切都是对象
1.对象存储位置 对象的引用存在栈中,对象存在堆中.new 出来的对象都在堆中存储.栈的存取速度较快. 所有局部变量都放在栈内存里,不管是基本类型变量还是引用类型变量,都存储在各自的方法栈中: 但是引 ...
- 2017/3/7 值得"纪念"的错误
使用viewpager和fragment做个能左右滑动的效果,结果怎么弄怎么有问题,先是怀疑什么viewPager维护刷新内部fragment什么的,又是在FragmentPageAdapter的ge ...
- java类型强转
知乎: 首先基本数据类型不是对象,强转改的是值,分为有损和无损,有损会丢失数据细节. 然后对象,只有继承关系的类才能强转,改变的只是引用,而且向上转型是安全的,把你转为人类是安全的,你还是你,只是现在 ...
- Python2.5/2.6实用教程:基础篇(转)
介绍 本文方便有经验的程序员进入Python世界.本文适用于python2.5/2.6版本. Deparecated:Python3 和 python2.x语法不同.如果你正在用python3,请参考 ...