[root@m01 ~]# rsn_count="ps -ef|grep 'rsync --d[a]emon'|wc -l"
[root@m01 ~]# echo ${rsn_count}
ps -ef|grep 'rsync --d[a]emon'|wc -l
[root@m01 ~]# eval ${rsn_count} 变量多次获取值得思路: 定义变量 每次执行的时候就执行 eval ${rsn_count}
每次获取的都是新值, 第二种思路: 一开始
rsn_count=$(ps -ef|grep 'rsync --d[a]emon'|wc -l) 获取变量
然后再次获取的时候 执行命令ps -ef|grep 'rsync --d[a]emon'|wc -l 获取
[root@m01 ]# cat ryn_srv.sh #!/bin/bash
rsn_file="/usr/bin/rsync"
# 判断启动的进程数 [ -e /etc/init.d/functions ] && source /etc/init.d/functions || echo "/etc/init.d/functions 不存在"
[ -e ${rsn_file} ] || {
echo "${rsn_file} 服务不存在"
exit
}
args1=$ function start() {
if [ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -gt ]
then
echo "rsync 已经启动" else ${rsn_file} --daemon
sleep
[ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -gt ] && action "rsync 启动成功" /bin/true || echo "启动失败" fi } function stop() { if [ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -gt ]
then
killall rsync
killall rsync &>/dev/null
sleep
[ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -lt ] && action "rsync 关闭成功" /bin/true || echo "关闭失败" else echo "rsync 已经关闭" fi } function restart() {
stop
sleep
start
} case $args1 in
start)
start
;;
stop) stop
;; restart) restart
;; *)
echo "Usage {start|stop|restart}"
;; esac