Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to log out of a Linux system from a script

How-To
Jul 29, 20244 mins
Linux

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.

Night business woman with computer on glasses reflection for company innovation or eyes healthcare in digital, information technology career. Corporate manager reading, coding or software development
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.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author