Reputation: 3066
I have a shell script
that is used to setup the network settings on a linux machine in bash
, it will mainly be used over SSH. Here are the last few lines of the script.
service network stop
rm -rf $NETWORKFILE
touch $NETWORKFILE
echo NETWORKING=yes > $NETWORKFILE
echo HOSTNAME=$HOSTNAME >> $NETWORKFILE
mv $ETHFILE /etc/sysconfig/network-scripts/ifcfg-eth0
service network start
As you can see, to apply the network settings it has to stop then start the network and apply the settings while the network is down. This would then cause the SSH session to be disconnected on the first line of the code I have shown and the script to thus stop and the settings to not be applied. How can I have the shell script run these last few lines after the SSH session is disconnected that started the shell script? Also, it needs to be done in the code and not through a screen
or nohup
command when starting the script.
Upvotes: 0
Views: 92
Reputation: 768
Try completely disconnecting the script from the terminal, by redirecting all standard streams and putting it in background:
nohup script < /dev/null > script.log 2>&1 &
Also you can put "sleep 2" as the first line of the script, so that after putting the script in background, you can quickly disconnect cleanly, before the server closes it forcibly. This is just for convenience.
Upvotes: 1
Reputation: 838
Maybe if you get your script PID, and then disown the process after stopping the network, your script will continue running after the ssh session is disconnected.
./script &
pid=$$ disown -h $pid service network stop rm -rf $NETWORKFILE touch $NETWORKFILE echo NETWORKING=yes > $NETWORKFILE echo HOSTNAME=$HOSTNAME >> $NETWORKFILE mv $ETHFILE /etc/sysconfig/network-scripts/ifcfg-eth0 service network start
Upvotes: 0