How to open apps from bash (Linux)

Viewed 613

Automate part of your life where you can! (It doesn't seem to be working properly...). This time is for opening multiple apps on my desktop while I make my morning coffee.

I created a .sh to open my workspace apps like Visual Code, Slack, Firefox, etc.

The script is basically:

exec code
exec /usr/local/firefox_dev/firefox
exec snap/slack/39/usr/bin/slack
exec /snap/spotify/current/usr/share/spotify/spotify

I basically found the paths to the executable apps and put exec in front of them... And yes, I chmod +x ./workspace-apps-script.sh to make it executable.

The problem is: No matter which exec line I put first, it only executes the first line. I would like them to open one-by-one and open til the last one.

3 Answers

The problem is that you apps do not return when launched. So you have to let they go in background or detached.

To execute a command in background you need just to add & at the end of the command line in the script. For example:

#!/bin/bash

code &
/usr/local/firefox_dev/firefox &
snap/slack/39/usr/bin/slack &
/snap/spotify/current/usr/share/spotify/spotify &

If you want to execute the command in detached mode you can use nohup at the beginning of the command line. For example:

#!/bin/bash

nohup code
nohup /usr/local/firefox_dev/firefox
nohup snap/slack/39/usr/bin/slack
nohup /snap/spotify/current/usr/share/spotify/spotify

I suggest you to combine the two mode to get the best result. So you can do this in your script:

#!/bin/bash

nohup code &
nohup /usr/local/firefox_dev/firefox &
nohup snap/slack/39/usr/bin/slack &
nohup /snap/spotify/current/usr/share/spotify/spotify &

However if you want to execute this every time you login to you pc you can write the abobe lines directly in you .bashrc file That is in your home directory and is executed at every login.

remove exec from the beginning (just call the programs directly):

#!/bin/bash

code
/usr/local/firefox_dev/firefox
snap/slack/39/usr/bin/slack
/snap/spotify/current/usr/share/spotify/spotify

You can always use shellcheck -s bash <your-script-path> to see if there are some errors.

If you're new to bash and shell scripting in general, then exec is not what you assume to be. It is not for executing programs. exec is a shell builtin command that replaces the current executing process (in this case, shell) with the program you pass to exec.

So, exec /usr/local/firefox_dev/firefox replaces/overwrites your shell process with the firefox program, but it does not create a new process. To understand the difference, see below commands:

$ # At the Command Prompt
$ # To see running processes, call ps
$ ps
PID   TTY         TIME CMD
6017  pts/2   00:00:00 ps
19890 pts/2   00:00:01 bash
$ 
$ exec /usr/local/firefox_dev/firefox # Replaces bash with firefox program and firefox will now have PID of bash which is 19890 in this example

And importantly, the program does not return control to the caller program (shell) since the caller process no longer exists. So, in your case, that's the reason only the first program runs but not the remaining ones. If you do not call a program with exec, it gets a new PID (process ID) and the called program becomes a child process of the caller process (shell in this case)

Related