You Can’t Use logout in a Bash Script, So Use These Workarounds


Key Takeaways

  • The Linux logout command doesn’t work in scripts because they run in non-login shells.
  • In a login shell in a console, you can use the exec command to launch the script and you’ll be logged out when it terminates.
  • Alternative methods include using source or appending the logout command to the command line.



The Linux logout command doesn’t work inside Bash scripts. It’s rare but, occasionally, you may need that functionality. Here are three ways to sidestep the issue and log out from a script.


Why logout Doesn’t Work in Scripts

There are login shells and non-login shells. The very first shell that is created when you log in is a login shell. All other shells are not login shells. The differences between the two types of shell stem from the different system files that are read by the shell as it is created.

Plainly, you can’t log out of a non-login shell. If a shell has no concept of logging in, it can’t log you out.

Shell scripts don’t run in the shell they’re launched from. A new shell is spawned for them to run in. Even if they were launched from a login shell, they don’t run in a login shell.


We can see this quite easily. This is the login prompt of a computer running Arch Linux, without any graphical desktop environment installed. It’s using the Bash shell. I’ve logged in, and I’m about to hit Enter on the logout command.

logout 
Running the logout command on a computer without a graphical desktop environment installed.

As you’d expect, I’m dumped back at the login prompt.

The login prompt on a computer without a graphical desktop environment installed.

So evidently, the shell I’m using is a login shell. Let’s log back in, and check the contents of a script called lo.sh. Then we’ll run it.


cat lo.sh
./lo.sh
Running a script with the logout command in it, from a login shell.

Bash complains that we can’t use logout in a non-login shell. Even though our script was definitely launched from a login shell, it doesn’t work. We’re not logged out.

Nor will it work in a terminal emulator window on a computer with a graphical desktop environment. In this case, it’s Ubuntu with the GNOME desktop.

logout 
Trying to use the logout command in a terminal window with a non-login shell.


I said that you only have a single login shell. That’s not strictly the case. There are scenarios where you may have more than one login shell running on your computer, but you only have a single login shell per user instance.

Each user needs their own login shell, so they can log in and out. It’s possible to log in to the same computer, more than once, as the same user. If you have SSH set up on your computer to support remote access, you can log in again over SSH, to your local computer, through a terminal window.

ssh dave@localhost
who
logout
who
Using SSH to log in to the local computer.

Once we’ve logged in, we run the who command to list the current users, then log out and run who again. We can see that we temporarily had an extra user. That user has their own login shell. But that doesn’t help us with our scenario. They’re the only user who can access that shell, and they’ll see the same behavior as we’ve just seen.


How To logout Using exec

So: logout must be used in a login shell, but our script won’t run in a login shell even if it is launched from one.

What we can do is launch our script with exec. This doesn’t follow the normal process of creating a shell to run the script in. It replaces the current shell with the command that exec launches. The shell is discarded. When the command terminates, it can’t go back to the now non-existent shell. If we do this from a login shell, we’ll be logged out.

We need a script to experiment with. Copy this trivial script to your favorite editor, then save it as “lo.sh”.

#!/bin/bash

logout

We’ll use chmod to make it executable.

chmod +x lo.sh 
Using the chmod command to make the lo.sh script executable.


The script fails with the now familiar error message:

./lo.sh 
Running a script with the logout command in it, in a terminal window with a non-login shell.

If we launch it with exec, the terminal window is closed when the script stops running.

exec ./lo.sh 
Using exec to launch a script that has logout in it, in a non-login shell.

Of course, on our console-only Arch computer, we’re not using a terminal emulator. exec replaces our login shell with the script.

Using exec to launch a script containing logout in a console window with a login shell.


When the script finishes, we’re presented with a new login prompt. So, it works for console logins on Linux computers without a graphical desktop environment installed. Does that help us on our Ubuntu computer that’s running GNOME?

Yes, it does. In GNOME, we can press Ctrl+Alt+FnKey to open exactly the same sort of full screen, login shell console. The FnKey can be one of F3 through F6.

Logging in to a console login shell on a computer running GNOME, and using exec to launch a script with logout in it.

We can log in and use the same command as we did on our Arch computer.

exec lo.sh 

We are logged out, and returned to the console login prompt. Pressing Ctrl+Alt+F1 returns you to your GNOME session. You’ll need to enter your password to regain access.


The Ubuntu GNOME login screen.

Note that you’re returned to your GNOME session the way it was before you pressed Ctrl+Alt+F3. Your GNOME session isn’t logged out, but it is password protected.

If you don’t want to have your GNOME session user left logged in, log them out before you run your script. You can press Ctrl+Alt+F3 on the Ubuntu login screen, sign in at the console, and run your script.

A couple of points. When you’re using exec like this, the logout command in the script is superfluous. It’s the fact that the script is terminating with no shell to go back to that effectively logs you out.


You could use exit instead of logout, or not use either command at all. Once the script terminates, you’re logged out. If you’ve got branches of execution in your script, and it can terminate in different places, you can use exit to terminate each of the possible paths of execution.

Another issue is, because you’re not executing your script inside a shell, you don’t have access to shell features like the expansion of wildcards, nor aliases and shell functions. If your script needs these, you can use one of the other techniques, below.

How To logout Using source

Using the Linux source command interprets the commands inside your script without launching the script in a shell. The commands are interpreted by the current shell.

Open a console window as above, using Ctrl+Alt+F3, and use the source command to interpret the commands in your script.


source ./lo.sh 
Using the source command to read and interpret the commands in a script that contains the logout command, in a login console shell.

You can use the word source, or a period.

. ./lo.sh 

With this technique you’ll need to use exit or logout the script. The termination of the script on its own won’t log you out.

How to logout Using a Bash Function

There’s a way to run any script and have it log out when it finishes, without calling logout or exit from the script itself. Save this script as simple.sh.

#!/bin/bash

echo "This script doesn't call any other commands."

Make it executable by typing:

chmod +x simple.sh 
Using chmod to make the simple.sh script executable.

In a login console screen, add the logout command after the script name. Separate the script name from the logout command with a semicolon.


./simple.sh; logout 
Appending the logout command to the command line that launches a script.

When our script terminates, we’re logged out.

If you do this frequently, it’d be convenient to make a small Bash function like this.

run-logout () {
  $1; logout
}

You can call this function and pass the name of a script to it. When you do this in a login console, you’ll be logged out when the script finishes.

run-logout simple.sh 

A Quick Round-Up

If you want to have a script running to completion in your absence, and have the user logged out when it completes so there are no user logged in at all, follow these steps.

  • Log all users out of the computer.
  • At the GNOME login in screen, press Ctrl+Alt+F3 to open a login console window.
  • Log in.
  • Launch your script with one of three methods shown above.



Source link

Previous articlePlaystation 5 Pro Arrives With a Graphics Boost and $700 Price Tag