Start Specific Terminal on Startup
I coded an application using libpcap and ncurses. Obviously, this application is supposed to run in the terminal.
Therefore, I created a script that launches my application in gnome-terminal with a fixed size:
#!/bin/sh
gnome-terminal --geometry=75x36+0+0 --command="myapplication"'
I want to execute that on startup (after the desktop of the current logged in user has shown up). It is important to run this as root. Otherwise, libpcap won't be able to sniff for packets. I am aware of all the risks involved.
I created the following .service
file:
[Unit]
Description=My Application
[Service]
User=root
Type=simple
ExecStart=/usr/local/bin/myshellfile
[Install]
WantedBy=multi-user.target
And I put it in /etc/systemd/system/myservice.service
and preset it with
sudo systemctl preset myservice.service
Nevertheless, it does not work. What am I doing wrong here? Running Ubuntu 16.04 in an updated state.
There are few possible methods to launch GUI application (as gmome-terminal) at system startup and maybe the most appropriate is to use Startup Applications (see also). In this way the app will be started when the user login. (In every other method we should export some desktop envvars to be able to do that.)
To be able to test my answer, I created executable file, called myapp
, located in /usr/local/bin
. It adds just the current date and time within a file in the /root
directory, so we will need root (sudo) permissions to run the script:
#!/bin/bash
date >> /root/date.txt
We can grant permissions to a specific user to run a command without password via sudo
. No matter whether the user belongs to the sudoers group or not. We can use sudo visudo
to edit safely /etc/sudoers
and add one or more lines as these:
user1 ALL=(ALL) NOPASSWD: /usr/local/bin/myapp
user2 ALL=(ALL) NOPASSWD: /usr/local/bin/myapp
The better idea is to create a separate file, for example /etc/sudoers.d/myapp
, where to put these rules. For this purpose we shall use the command: sudo visudo -f /etc/sudoers.d/myapp
.
Now user1
and user2
should be able to run sudo myapp
without password.
Note: Always use the command
visudo
to edit thesudoers
file to make sure you do not lock yourself out of the system – just in case you accidentally write something incorrect to thesudoers
file.visudo
will save your modified file to a temporary location and will only overwrite the realsudoers
file if the modified file can be parsed without errors... source.
The next step is to modify the startup script. Let's call it myapp_startup
and place it into the same directory /usr/local/bin
. The script content should be:
#!/bin/sh
gnome-terminal --geometry=75x36+0+0 -x bash -c "sudo myapp; exec bash"
- Detailed explanation about the above command is provided here.
- If you want to minimise the window automatically use: this approach.
Now we can create an entry in Startup Applications as this:
- Additionally we can modify the startup script in this way:
sleep 3 && gnome-terminal ...
to launch the new terminal window after the entire desktop is initialised. - When the script isn't located within a directory that is added to user's
$PATH
variable (echo $PATH
) we should use the full path, for example:/usr/local/bin/myapp_startup
.
Further reading:
Instead of gnome-terminal probably you can use
tmux
(or some other session manager) as it is described in this answer.Additionally you can launch the startup script via Cron (or some other limited environment) by the script
cron-gui-launcher.bash
from this project: https://github.com/pa4080/cron-gui-launcher. This script will wait the user to login and then will export all DE Variables, etc... But in the current case the final result will be the same as we using Startup Application.