shell脚本连接并重启远程服务器的方法

shell重启远程服务器

#connServer.sh 登陆服务器脚本 本地文件
#!/usr/bin/expect
expect -c "
spawn ssh ali@192.168.1.1
expect {
\"*assword\" {set timeout 30; send \"123456\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
send \"cd /home/ali/demoProject \r\"     #根据系统而定 maybe /User/ali
send \"sh reboot.sh \r\"
expect eof"

echo "重启成功!"

#reboot.sh 重启脚本 该文件是放在远程服务器的文件
#!/usr/bin/bash
ps aux | grep /home/ali/demoProject/index.js | awk '{print $2}' | xargs kill
ps -ef | grep /home/ali/demoProject/index.js
echo "success reboot!"

#下面这条命令以绝对路径启动 方便以后kill 该进程

nohup node /home/ali/demoProject/index.js &

不知道什么原因,如果想再connecServer.sh中发送reboot.sh 的脚本命令,

有一个问题 : $2 没了. 奇迹般的消失在命令行中.

现在采取折中的办法就是将这条带有$2的命令,放在服务器端单独一个文件里, 在本地执行这个reboot.sh文件.

相关推荐