- How to «correctly» start an application from a shell
- 3 Answers 3
- How to Run an Application in Linux
- How To Run an Application in Linux
- How To Run a Program Using Terminal in Linux
- How To Run Program Using Run Command In Linux
- How To Run Program using Application Menu
- How To run Program using Application Launcher Search
- How To Run Program Using Keyboard Shortcuts in Linux
- Conclusion
- About the author
- Syeda Wardah Batool
- Start applications
- Quickly running a command
How to «correctly» start an application from a shell
Update 1: I can illustrate what e.g. dmenu does reconstructing it from repeated calls to ps -efl under different conditions. It spawns a new shell /bin/bash and as a child of this shell the application /path/to/Program . As long as the child is around so long will the shell be around. (How it manages this is beyond me. ) In contrast if you issue nohup /path/to/Program & from a shell /bin/bash then the program will become the child of this shell BUT if you exit this shell the program’s parent will be the uppermost process. So if the first process was e.g. /sbin/init verbose and it has PPID 1 then it will be the parent of the program. Here’s what I tried to explain using a graph: chromium was launched via dmenu , firefox was launched using exec firefox & exit :
systemd-+-acpid |-bash---chromium-+-chrome-sandbox---chromium-+-chrome-sandbox---nacl_helper | | `-chromium---5*[chromium-+-] | | |-] | | |-] | | |-] | | `-3*[]] | |-chromium | |-chromium-+-chromium | | |- | | `- | |- | |-3*[] | |- | |-5*[] | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |-2*[] | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | `- |-2*[dbus-daemon] |-dbus-launch |-dhcpcd |-firefox-+-4*[] | |- | |- | |- | |-3*[] | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | |- | `- |-gpg-agent |-login---bash---startx---xinit-+-Xorg.bin-+-xf86-video-inte | | `- | `-dwm-+-dwmstatus | `-xterm---bash-+-bash | `-pstree |-systemd---(sd-pam) |-systemd-journal |-systemd-logind |-systemd-udevd |-wpa_actiond `-wpa_supplicant
Update 2: I guess the question can also be boiled down to: What should be the parent of a process? Should it e.g. be a shell or should it be the init process i.e. the process with PID 1 ?
dang, garbage — you ask some good questions. but I think wayne’s on the nose here — your latest edit asks about init — to which the answer might be. maybe? it depends on how/if you plan to talk to it, what init you use, and where the data channels are. In general that stuff will tend to work itself out — that’s what init is for. In any case, usually when you daemonize a process then init . Or if you want job control, current shell.
Hahaha, cheers @mikeserv; 4:37 am in the morning here and already the first laugh of the day. True, that stuff always somehow works out. I will remove dmenu and see how I get along with what I learned. I find exec /path/to/Program & exit or /bin/bash -c /path/to/Program & exit to be quite usable. But they all make 1 i.e. init the parent of the Program which is fine with me as long as this makes sense and does not violate any basic *nix principles.
@lord.garbage — that’s because you exec & , I think. I usually just do my stuff from the terminal. maybe you’d get some use out of ben crowell’s question here. I have an answer there, but all of them are very good. anyway, when you background a process and its parent dies like: sh -c ‘cat & kill $$’ you orphan it, and it winds up getting reaped eventually. that’s init’s job — that’s why they all fall to it.
Maybe a simpler question for now is: How is it possible to get the above process tree from the shell: systemd—bash—chromium . All methods I try will ultimately lead to a process tree of the following form systemd—chromium when I spawn firefox from the shell. How is the shell demonized here? It is not associated with any terminal.
3 Answers 3
There are a few ways way to execute a program and detach it from a terminal. One is to run it in the background of a subshell, like this (replace firefox with your favorite program):
Another is to disown the process:
If you are curious about how app launchers work, dmenu provides 1 binary and 2 shell scripts: dmenu , dmenu_path and dmenu_run , respectively.
dmenu_run pipes the output of dmenu_path into dmenu, which in turn pipes into whatever your $SHELL variable is set to. If it is empty, it will use /bin/sh .
dmenu_path is a bit more complex, but in short it provides a list of binaries in your $PATH environment variable, and uses a cache if possible.
#!/bin/sh cachedir=$ if [ -d "$cachedir" ]; then cache=$cachedir/dmenu_run else cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~ fi IFS=: if stest -dqr -n "$cache" $PATH; then stest -flx $PATH | sort -u | tee "$cache" else cat "$cache" fi
It isn’t necessary to have programs running in shells. Another way to write dmenu_run , without piping into a shell would be:
I like G-Man’s answer a lot. But I’m responding because I think you are confusing concerns. As Wayne points out, the best answer is «whatever gets the results you want».
In Unix process management, every process has a parent. The one exception to this is the init process which is started by the OS at boot. It is normal behavior for a parent process to take all of it’s child processes with it when it dies. This is done by sending the SIGHUP signal to all child processes; the default handling of SIGHUP terminates the process.
The shell spawning of user processes is no different than if you coded the fork(2)/exec(3) calls in the language of your choice. The shell is your parent, and if the shell terminates (e.g., you log off), then the child process(es) it spawns goes with it. The nuances you describe are just ways of modifying that behavior.
exec /path/to/program is like calling exec(3). Yes, it will replace your shell with program , keeping whatever parent launched the shell.
sh -c /path/to/program kind of pointlessly creates a child shell process that will create a child process of program . It is only valuable if /path/to/program is actually a sequence of script instructions, and not an executable file. ( sh /path/to/script.sh can be used to run a shell script that lacks execute permissions in an inferior shell)
/path/to/program creates a «foreground» process, meaning that the shell waits for the process to complete before taking any other action. In system call context, it is like fork(2)/exec(3)/waitpid(2). Note that the child inherits stdin/stdout/stderr from the parent.
/path/to/program & (ignoring redirection) creates a «background process». The process is still a child of the shell, but the parent is not waiting for it to terminate.
nohup /path/to/program invokes nohup(1) to prevent the sending of SIGHUP to program if the controlling terminal is closed. Whether it is in the foreground or background is a choice (although most commonly the process is backgrounded). Note that nohup.out is only the output if you don’t otherwise redirect stdout.
When you put a process in the background, if the parent process dies, one of two things happens. If the parent is a controlling terminal, then SIGHUP will be sent to the children. If it is not, then the process may be «orphaned», and is inherited by the init process.
When you redirect input/output/error, you are simply connecting the file descriptors that every process has to different files than the ones it inherits from its parent. None of this affects process ownership or tree depth (but it always makes sense to redirect all 3 away from a terminal for background processes).
With all that said, I don’t think you should be concerned with process creation by the shell or sub-shells or sub-processes, unless there is a specific problem you are addressing related to process management.
How to Run an Application in Linux
Launching an application using an operating system is the basic task that anyone can perform. The working process of the Linux operating system is totally different from other operating systems like Windows OS or macOS. If you’re a Linux beginner, and want to know different ways to launch or run an application, this tutorial will show you all easy and possible ways.
How To Run an Application in Linux
Linux distribution always has several solutions to perform one operation. You will find multiple choices and can select any of them.
To run an application/program in Linux distributions can be done using command-line and GUI as well. These are the tricks we will cover in this tutorial to run a program:
(I am using Linux Mint 21 to explain these methods)
How To Run a Program Using Terminal in Linux
Most beginners might think that launching an application through GUI is easy. But when you use the command-line, it looks even easier.
To open the application through command-line, open the terminal and type the program name:
How To Run Program Using Run Command In Linux
The “run command” is an alternative way to get apps quickly without using a terminal. All distributions of Linux have run commands as a built-in tool. It is quite simple, when you press “alt+f2”, a dialogue box will appear instantly with a pop-up “Enter a Command”:
Enter a program name in the dialogue box and hit enter:
How To Run Program using Application Menu
Like Windows, an application launcher is the most common way to run programs. Application launcher has a list of several icons that you can run on time. As we are working on Linux Mint, you will see the application launcher at the bottom left corner of the screen. To run the application, simply navigate to that application icon and click it:
How To run Program using Application Launcher Search
Application menu is yet another easiest way to run programs on screen. What you need to do is open the application menu by clicking its icon located at the top left corner of the screen:
It will redirect you to the Software Manager, navigate towards the search bar and type the program name with correct spelling and select it to open:
How To Run Program Using Keyboard Shortcuts in Linux
If you’re a keyboard user and don’t prefer to use a mouse, then this trick will help you. Keyboard shortcuts allow the user to run programs without using a mouse. It would be done when paying attention to the few steps we have mentioned below:
Open the application menu, type keyboard in the search bar and hit Enter:
Move to the “Shortcuts” tab, select “Launchers” from the left corner and you will get “Custom Shortcuts” there, hit it and add any shortcut you want.
Conclusion
The Linux operating system provides multiple ways to the beginner. It helps them to learn each way and select their favorite one to perform the task. Similarly, this article has shown multiple tricks to run or launch a program in Linux. We have mentioned 6 easiest approaches to run or launch a program on a Linux system.
About the author
Syeda Wardah Batool
I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.
Start applications
Move your mouse pointer to the Activities corner at the top left of the screen to show the Activities overview. This is where you can find all of your applications. You can also open the overview by pressing the Super key.
There are several ways of opening an application once you’re in the Activities overview:
- Start typing the name of an application — searching begins instantly. (If this doesn’t happen, click the search bar at the top of the screen and start typing.) If you don’t know the exact name of an application, try to type an related term. Click the application’s icon to start it.
- Some applications have icons in the dash , the horizontal strip of icons at the bottom of the Activities overview. Click one of these to start the corresponding application. If you have applications that you use very frequently, you can add them to the dash yourself.
- Click the grid button (which has nine dots) in the dash. You will see the first page of all installed applications. To see more applications, press the dots at the bottom, above the dash, to view other applications. Press on the application to start it.
- You can launch an application in a separate workspace by dragging its icon from the dash, and dropping it onto one of the workspaces. The application will open in the chosen workspace. You can launch an application in a new workspace by dragging its icon to an empty workspace, or to the small gap between two workspaces.
Quickly running a command
Another way of launching an application is to press Alt + F2 , enter its command name , and then press the Enter key.
For example, to launch Rhythmbox , press Alt + F2 and type ‘ rhythmbox ’ (without the single-quotes). The name of the app is the command to launch the program.
Use the arrow keys to quickly access previously run commands.