Exiting a script and logging out are very different, but sometimes you may need to log out of your Linux system from within a script. Here's how to do it. Credit: Shutterstock / PeopleImages.com - Yuri A Most of the time, exiting a script leaves you sitting at the command prompt. If you need to log out of your Linux system from within a script, things get a little complicated. Exiting a script and logging out are very different. There are, however, two ways to manage this. First, it’s important to understand that using the logout command in a script will generate an error message that tells you to use the exit command instead. Here’s an example. The script shown below was intended to log out the person running it once three users were logged into the system. Since the logout command isn’t one that can normally be used within a script, this generates errors. #!/bin/bash while true do sleep 10 count=`who | wc -l` echo $count if [ $count -ge 3 ]; then logout fi done Here’s what those errors would look like: $ wait4three 3 ./tryme2: line 9: logout: not login shell: use `exit' 3 ./tryme2: line 9: logout: not login shell: use `exit' The logout command will not log you out of the system from within a script. The errors shown above clearly tell us that we’re supposed to use the exit command and that logout isn’t going to work to exit the script or to log you out. And, as you undoubtedly know, exiting a script wouldn’t exit the shell. Using the exec builtin In spite of these constraints, however, there is one way to ensure that a logout command will run from a script, and that is to run the script using the exec command like this: $ exec wait4three The exec builtin is an unusual Linux command. It allows you to execute a command from bash itself. It doesn’t need to create a new process. Instead, it replaces bash with the command to be run. If the exec command is successful, it does not return to the calling process. If you run a script using a command like exec myscript and the script includes a logout command, the script will end abruptly and so will the shell from in which it was running. Using the source builtin Another way to log out of a system through a script is to use the source builtin to run the script. When you run a command such as source wait4three, the command will read and execute all the commands included in the script just as if you were typing them. In other words, the constraints involved in running a script are removed. Another benefit of using the source builtin is that you don’t need execute permission to run a script, just read access. One simple alternative One very simple alternative to logging out from inside a script is to run both the script and the logout command on one line – separated by a semicolon. This ensures that the logout command will run once the script has finished. $ wait4three; logout The script shown earlier could then use exit instead of logout. #!/bin/bash while true do sleep 10 count=`who | wc -l` echo $count if [ $count -ge 3 ]; then exit fi done Wrap-up While logging out of a system from a script is rarely required, there may be times when you need to run a lengthy process and want to be sure that you’re logged out when it completes. Running a script that includes the logout command and running with the exec or the source builtin makes this possible. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe