本人在写脚本重启动过程遇到坑,分享给大家,避免大家误入同样的坑。
[root@test scripts]# more squid.sh
#!/bin/bash
source /etc/profile
kill -9 $(ps -ef |grep "squid" |grep -v "grep" |awk '{print $2}')
ps aux |grep squid
sleep 1
/usr/sbin/squid -f /etc/squid/squid.conf
ps aux |grep squid
进程图

[root@test scripts]# sh -x squid.sh
+ source /etc/profile
++ '[' -x /usr/bin/id ']'
++ '[' -z 0 ']'
+++ /usr/bin/id -un
++ USER=root
++ LOGNAME=root
++ MAIL=/var/spool/mail/root
++ '[' 0 = 0 ']'
++ pathmunge /usr/sbin
...
...
+++ alias 'which=alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
++ unset i
++ unset -f pathmunge
+++ whoami
++ export 'HISTTIMEFORMAT=%F %T root '
++ HISTTIMEFORMAT='%F %T root '
+ stop
++ ps -ef
++ grep --color=auto squid
++ grep --color=auto -v grep
++ awk '{print $2}'
+ kill -9 15556 15561 15565 16376 16397
Killed
由于执行squid.sh 脚本时,直接将当前shell脚本一起kill 掉了。造成后续启动服务命令没有执行。


#!/bin/bash
source /etc/profile
kill -9 $(ps -ef |grep "/usr/sbin/squid -f /etc/squid/squid.conf" |grep -v "grep" |awk '{print $2}')
kill -9 $(ps -ef |grep "/var/log/squid/access.log" |grep -v "grep" |awk '{print $2}')
ps aux |grep squid
sleep 1
/usr/sbin/squid -f /etc/squid/squid.conf
echo "squid is restart $(date)" >> /data/logs/squid.log
