0

I am wanting to run a bash script on startup in Ubuntu 20.04 with the terminal visible. The test.sh file is located at /usr/bin/test.sh. I can get the test.sh file to run at startup but not in a visible terminal window.

Contents of test.sh:

#! /bin/bash
echo "hello";

I can not get it to work, I have tried (individually):

Crontab (with and without the '&' and with/without "sudo")

@reboot bash test.sh &

@reboot /usr/bin/test.sh &

@reboot DISPLAY=:0 xterm -hold -e bash -c "bash test.sh" &

@reboot DISPLAY=:0 xterm -hold -e bash -c "bash /usr/bin/test.sh" &

Startup Applications Command

sudo bash /usr/bin/test.sh
bash /usr/bin/test.sh
/usr/bin/test.sh

Creating a Service at /etc/systemd/system/testService.service

[Unit]
Description = Test Service

[Service] WorkingDirectory= /usr/bin ExecStart= /usr/bin/test.sh

[Install] WantedBy=multi-user.target

And start, enable and checked status..

systemctl start testService.service
systemctl enable testService.service
systemctl status testService.service

But failed to start.

Any help / pointing in a better direction would be appreciated!

Black Solis
  • 11
  • 1
  • 4

2 Answers2

1

To get a GUI terminal window to appear when you run your script:

Add to "Startup Applications" (under command):

bash test.sh

Contents of test.sh:

#! /bin/bash
    DISPLAY=:0.0 xterm -hold -e bash helloWorld.sh

Contents of helloWorld.sh:

#! /bin/bash
echo "hello";

For me, this opened an XTerm terminal window upon login and ran the helloWorld.sh script.

Black Solis
  • 11
  • 1
  • 4
0

Try the below. Create a new script as below (call it say on_load.sh) who's job is to load your actual script. You can make that script run upon booting by putting an entry in your ~/.config/autostart

I am not sure if Ubuntu has autostart in the settings. I did mine in KDE Plasma and there its automated.

#! /bin/bash
konsole --noclose --workdir ~/usr/bin/ -e 'bash -c "~/usr/bin/test.sh; exec bash"' &

If you can't figure out how to put the above script in ~/.config/autostart, here is how it looks on my file. KDE Plasma created a new file inside my ~/.config/autostart called on_load.sh.desktop

That file looks like this:

[Desktop Entry]
Exec=/home/my_username/Documents/scripts/on_load.sh
Icon=dialog-scripts
Name=on_load.sh
Path=
Type=Application
X-KDE-AutostartScript=true

You need to ensure you have permissions to run both scripts.