Cent OS 7 chfs 以服务的方式开机启动
- chfs
- 制作系统服务文件 chfs
- 开机启动 chfs.service
- 服务常用操作指令
- 最终效果
chfs
chfs官网CuteHttpFileServer/chfs是一个免费的、HTTP协议的文件共享服务器,使用浏览器可以快速访问。它具有以下特点:
- 单个文件,核心功能无需其他文件
- 跨平台运行,支持主流平台:Windows,Linux和Mac
- 界面简洁,简单易用
- 支持扫码下载和手机端访问,手机与电脑之间共享文件非常方便
- 支持账户权限控制和地址过滤
- 支持快速分享文字片段
- 支持webdav协议
与其他常用文件共享方式(如FTP,飞秋,网盘,自己建站)相比,具有使用简单,适用场景更多的优点,在个人使用以及共享给他人的场景中非常方便快捷。
具体使用方法就不具体介绍了,官方很齐全
使用建议:Windows提供了带界面的程序,可以通过该程序完成所有配置将生成的配置文件直接拿过来使用,就不需要手动写配置文件了
制作系统服务文件 chfs
在 /etc/init.d/ 下新建一个chfs文件,写入如下内容
注意:内容已修改完成,如果路径不一致,修改路径即可。如果是其他程序,修改名称及路径路径,注意参数路径
#!/bin/bash
# chkconfig: - 85 15
# description: chfs is a World Wide Web server. It is used to serve
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
ProgramName="chfs"
ProgramPath="/opt/chfs/"
NAME=chfs
BIN=${ProgramPath}/${NAME}
CONFIGFILE=${ProgramPath}chfs.ini
SCRIPTNAME=/etc/init.d/${NAME}
version="2.0"
program_version=`${BIN} --version`
RET_VAL=0
[ -x ${BIN} ] || exit 0
strLog=""
fun_clangcn()
{
echo ""
echo "+---------------------------------------------------------+"
echo "| Manager for ${ProgramName}, Author Brokenway |"
echo "+---------------------------------------------------------+"
echo ""
}
fun_check_run(){
PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
if [ ! -z $PID ]; then
return 0
else
return 1
fi
}
fun_load_config(){
if [ ! -r ${CONFIGFILE} ]; then
echo "config file ${CONFIGFILE} not found"
return 1
fi
}
fun_start()
{
if [ "${arg1}" = "start" ]; then
fun_clangcn
fi
if fun_check_run; then
echo "${ProgramName} (pid $PID) already running."
return 0
fi
fun_load_config
echo -n "Starting ${ProgramName}(${program_version})......"
${BIN} --file=${CONFIGFILE} >/dev/null 2>&1 &
sleep 1
if ! fun_check_run; then
echo " failed"
return 1
fi
echo " Success"
echo "${ProgramName} (pid $PID)is running."
return 0
}
fun_stop(){
if [ "${arg1}" = "stop" ] || [ "${arg1}" = "restart" ]; then
fun_clangcn
fi
if fun_check_run; then
echo -n "Stoping ${ProgramName} (pid $PID)... "
kill $PID
if [ "$?" != 0 ] ; then
echo " failed"
return 1
else
echo " done"
fi
else
echo "${ProgramName} is not running."
fi
return 0
}
fun_restart(){
fun_stop
fun_start
}
fun_status(){
PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
if [ ! -z $PID ]; then
echo "${ProgramName} (pid $PID) is running..."
else
echo "${ProgramName} is stopped"
exit 0
fi
}
checkos(){
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
OS=CentOS
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
OS=Debian
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
OS=Ubuntu
elif grep -Eqi "Alpine" /etc/issue || grep -Eq "Alpine" /etc/*-release; then
OS=Alpine
else
echo "Not support OS, Please reinstall OS and retry!"
return 1
fi
}
fun_config(){
if [ -s ${CONFIGFILE} ]; then
vi ${CONFIGFILE}
else
echo "${ProgramName} configuration file not found!"
return 1
fi
}
fun_version(){
echo "${ProgramName} version ${program_version}"
return 0
}
fun_help(){
${BIN} --help
return 0
}
arg1=$1
[ -z ${arg1} ]
case "${arg1}" in
start|stop|restart|status|config)
fun_${arg1}
;;
[vV][eE][rR][sS][iI][oO][nN]|-[vV][eE][rR][sS][iI][oO][nN]|--[vV][eE][rR][sS][iI][oO][nN]|-[vV]|--[vV])
fun_version
;;
[Cc]|[Cc][Oo][Nn][Ff]|[Cc][Oo][Nn][Ff][Ii][Gg]|-[Cc]|-[Cc][Oo][Nn][Ff]|-[Cc][Oo][Nn][Ff][Ii][Gg]|--[Cc]|--[Cc][Oo][Nn][Ff]|--[Cc][Oo][Nn][Ff][Ii][Gg])
fun_config
;;
[Hh]|[Hh][Ee][Ll][Pp]|-[Hh]|-[Hh][Ee][Ll][Pp]|--[Hh]|--[Hh][Ee][Ll][Pp])
fun_help
;;
*)
fun_clangcn
echo "Usage: $SCRIPTNAME {start|stop|restart|status|config|version}"
RET_VAL=1
;;
esac
exit $RET_VAL
在目录/etc/rc3.d/ 下,新建一个指向在第2步中建立的服务文件的符号连接文件。
打开目录/etc/rc3.d/ ,我们将看到这里都是符号连接文件(linux系统中的符号连接相当于windows中的快捷方式)。采用以下命令格式:
ln -sf [目标文件名] [连接文件名]
举例,以安装 chfs 服务为例,输入以下代码:
cd /etc/rc3.d
ln -sf /opt/chfs/chfs S10chfs
完成以上3个步骤,你就把自己的一个可执行程序做成系统的一个服务了,它具有我们一开始说的几个特定。我们可以用service命令控制这个程序了:
启动:service chfs start
停止:service chfs stop
重启:service chfs restart
上面的chfs 为服务名,也就是在第2步中建立的文件名。
开机启动 chfs.service
vim /lib/systemd/system/chfs.service
写入如下内容
[Unit]
Description=chfs
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/chfs start
ExecReload=/etc/init.d/chfs restart
ExecStop=/etc/init.d/chfs stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
服务常用操作指令
systemctl start chfs.service #开启服务
systemctl enable chfs.service #开机启动
systemctl disable chfs.service #禁止开机启动
systemctl status chfs.service #查看服务状态
systemctl restart chfs.service #重启服务
#查看所有已启动的服务:
systemctl list-units --type=service
systemctl list-unit-files
最终效果
[centos@localhost ~]$ systemctl start chfs
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: centos
Password:
==== AUTHENTICATION COMPLETE ===
[centos@localhost ~]$ systemctl status chfs
● chfs.service - chfs
Loaded: loaded (/usr/lib/systemd/system/chfs.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-06-16 10:00:59 CST; 8s ago
Process: 3361 ExecStart=/etc/init.d/chfs start (code=exited, status=0/SUCCESS)
Main PID: 3372 (chfs)
Tasks: 8
CGroup: /system.slice/chfs.service
└─3372 /opt/chfs//chfs --file=/opt/chfs/chfs.ini
Jun 16 10:00:58 localhost.localdomain systemd[1]: Starting chfs...
Jun 16 10:00:58 localhost.localdomain chfs[3361]: 2.0
Jun 16 10:00:58 localhost.localdomain chfs[3361]: +---------------------------------------------------------+
Jun 16 10:00:58 localhost.localdomain chfs[3361]: | Manager for chfs, Author Brokenway |
Jun 16 10:00:58 localhost.localdomain chfs[3361]: +---------------------------------------------------------+
Jun 16 10:00:59 localhost.localdomain chfs[3361]: Starting chfs()...... Success
Jun 16 10:00:59 localhost.localdomain chfs[3361]: chfs (pid 3372)is running.
Jun 16 10:00:59 localhost.localdomain systemd[1]: Started chfs.
chfs 文件访问效果