If you happen to run a script utilizing a command like exec myscript and the script features a logout command, the script will finish abruptly and so will the shell from wherein it was operating.
Utilizing the supply builtin
One other option to sign off of a system via a script is to make use of the supply builtin to run the script. Whenever you run a command corresponding to supply wait4three, the command will learn and execute all of the instructions included within the script simply as when you have been typing them. In different phrases, the constraints concerned in operating a script are eliminated.
One other advantage of utilizing the supply builtin is that you simply don’t want execute permission to run a script, simply learn entry.
One easy different
One quite simple different to logging out from inside a script is to run each the script and the logout command on one line – separated by a semicolon. This ensures that the logout command will run as soon as the script has completed.
$ wait4three; logout
The script proven earlier may then use exit as a substitute of logout.
#!/bin/bash whereas true do sleep 10 depend=`who | wc -l` echo $depend if [ $count -ge 3 ]; then exit fi performed
Wrap-up
Whereas logging out of a system from a script is never required, there could also be occasions when it is advisable to run a prolonged course of and wish to make sure that you’re logged out when it completes. Operating a script that features the logout command and operating with the exec or the supply builtin makes this doable.