Startup scripts in Mint 19.x

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Startup scripts in Mint 19.x

Post by catweazel »

Objective
This tutorial shows you how to correctly implement a startup script in Mint 19.

Background
It used to be that you could implement a simple startup script in rc.local but that is now frowned upon. In addition, third party services that would start automatically under Mint 18.x will not necessarily automatically start under Mint 19.x.

To correctly implement a startup script you need to create a systemd unit (service, if you prefer).

About the example
I run hardware RAID using Adaptec 6805TQ cards (under $US50 brand new on FleaBay). Adaptec provide a browser-based storage manager (running on Apache Tomcat's Catalina servelet) that used to auto-start under Mint 18, but after installing and rebooting on Mint 19, the storage manager no longer auto-starts, hence the need for me to create a systemd unit to auto-start the web server. You can modify the example below quite easily for your own needs.

Prerequisites
All you need is the script to be run. In the example, I'm using /usr/StorMan/StorAgnt.sh but you can reference any script you like.

Step 1
Create the initial service definition:

sudo nano /etc/systemd/system/storage.service

Substitute your service name for storage in the above example.

Add the following text to the newly created file:

Code: Select all

[Unit]
After=network.target

[Service]
ExecStart=/usr/StorMan/StorAgnt.sh

[Install]
WantedBy=default.target
Use ctrl-o + Enter to save the file. Use ctrl-x to exit. Replace /usr/StorMan/StorAgnt.sh with the fully qualified path and name of your script.

In the example above:

After: Tells systemd when the script should be run. In this case, the script will run after the network has started.
ExecStart: Is the fully qualified path to the script you want to execute.
WantedBy: Specifies the boot target that your new systemd unit should be installed to.

See man systemd.service for more information.

Step 2
Set the execute permissions on your script:

sudo chmod 744 /usr/StorMan/StorAgnt.sh

See this link for information about chmod 774

Step 3
Change the access mode permissions of your service definition:

sudo chmod 664 /etc/systemd/system/storage.service

See this link for information about chmod 664

Step 4
Prepare systemd to run your service:

Code: Select all

sudo systemctl daemon-reload
sudo systemctl enable storage.service
At this point you should have a message to the effect:

Code: Select all

Created symlink from /etc/systemd/system/default.target.wants/storage.service to /etc/systemd/system/storage.service.
Step 5
Start the service:

sudo systemctl start storage.service

If all went well, your script has been executed. To test further, reboot your system.

That's all there is to it.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
User avatar
ugly
Level 5
Level 5
Posts: 592
Joined: Thu Nov 24, 2016 9:17 pm

Re: Startup scripts in Mint 19.x

Post by ugly »

Thanks for the helpful tutorial and additional information links.

I've been able to make a few startup scripts using systemd units.

I'm too new to Linux to understand why some people think systemd is the devil. In my, limited, experience, getting scripts to run at shutdown and startup using systemd was a lot easier and more reliable than making scripts in init.d and the various rc folders.
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Startup scripts in Mint 19.x

Post by catweazel »

ugly wrote: Sun Aug 12, 2018 12:30 am Thanks for the helpful tutorial and additional information links.
You're most welcome.
ugly wrote:I've been able to make a few startup scripts using systemd units.

I'm too new to Linux to understand why some people think systemd is the devil. In my, limited, experience, getting scripts to run at shutdown and startup using systemd was a lot easier and more reliable than making scripts in init.d and the various rc folders.
Some linux purists despise systemd. This user puts it quite succinctly:
People hate systemd because it’s a pain in the ass to maintain, it’s complex, and defies the UNIX Philosophy. Which triggers UNIX purists, and it’s very heavy compared to other init systems such as OpenRC, and Runit.

On the grander scheme of things it’s simple for the user, but difficult for the developers. Despite it being implemented on the top popular distros (Debian, Ubuntu, Arch, OpenSUSE, Fedora, etc etc… On a relevant side-note, it’s not so secure like other init systems. As previously seen with Systemd’s vulnerability issues people have reported on.
https://forum.level1techs.com/t/whats-t ... d/119256/4
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Ted3658

Re: Startup scripts in Mint 19.x

Post by Ted3658 »

Will this work if a script asks for a password?

I want to run a script at start up that asks for a pw, and I know I can't pipe in the pw, but I'd be happy if at start up it would automatically open terminal, run the script, and then just sit there at the pw prompt waiting for me to put in my pw.

Thanks.
george33

Re: Startup scripts in Mint 19.x

Post by george33 »

May be exist automate tools for all that boring steps? Just enter name of command file and name of service and the others can do the computer (tool). Or it is a rocket science on Linux? :) P. S. I newb in Linux and such issues too weird for me.
User avatar
BG405
Level 9
Level 9
Posts: 2507
Joined: Fri Mar 11, 2016 3:09 pm
Location: England

Re: Startup scripts in Mint 19.x

Post by BG405 »

Thanks for posting this useful stuff Catweazel. Look forward to seeing you on here when you're back from your travels! :)
Dell Inspiron 1525 - LM17.3 CE 64-------------------Lenovo T440 - Manjaro KDE with Mint VMs
Toshiba NB250 - Manjaro KDE------------------------Acer Aspire One D255E - LM21.3 Xfce
Acer Aspire E11 ES1-111M - LM18.2 KDE 64 ----Two ROMS don't make a WRITE
Namf_Mint

Re: Startup scripts in Mint 19.x

Post by Namf_Mint »

One can also run a script via Mint's 'Startup Applications' tool.
User avatar
BG405
Level 9
Level 9
Posts: 2507
Joined: Fri Mar 11, 2016 3:09 pm
Location: England

Re: Startup scripts in Mint 19.x

Post by BG405 »

Namf_Mint wrote: Sat Jan 12, 2019 10:36 pm One can also run a script via Mint's 'Startup Applications' tool.
Indeed, but if I'm not mistaken, Catweazel's approach allows services etc. to be started in precise order and not just following set timeouts post login. :wink:
Dell Inspiron 1525 - LM17.3 CE 64-------------------Lenovo T440 - Manjaro KDE with Mint VMs
Toshiba NB250 - Manjaro KDE------------------------Acer Aspire One D255E - LM21.3 Xfce
Acer Aspire E11 ES1-111M - LM18.2 KDE 64 ----Two ROMS don't make a WRITE
george33

Re: Startup scripts in Mint 19.x

Post by george33 »

Namf_Mint wrote: Sat Jan 12, 2019 10:36 pm One can also run a script via Mint's 'Startup Applications' tool.
Yep it works even for services as has to be! Thanks!
amendoza

Re: Startup scripts in Mint 19.x

Post by amendoza »

Thanks a lot! Had been having some troubles with the anchor from unbound not functioning properly, so here it is the service config, modified with your steps, and unbound-anchor now properly executes after I have network online :D

[Unit]

Description=Restart unbound.service after network online, to help with the anchor bug. --By asomarriba

After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/bin/systemctl restart unbound.service
StandardOutput=journal

[Install]
WantedBy=default.target
PaulCarry

Re: Startup scripts in Mint 19.x

Post by PaulCarry »

Perfect. Was stuck on a startup script problem earlier today and this seems to have solved it. Trying to get my head back into Linux is proving problematic at the moment.
marciozuchini
Level 1
Level 1
Posts: 3
Joined: Fri May 29, 2020 11:31 am

Re: Startup scripts in Mint 19.x

Post by marciozuchini »

Many thanks!
I was struggling in order to run a script before the login (which can be provided by 'Startup Applications' tool) and your post was the best tutorial I've found so far.
ZoltanG
Level 1
Level 1
Posts: 10
Joined: Mon Sep 16, 2019 3:02 am

Re: Startup scripts in Mint 19.x

Post by ZoltanG »

Thanks for this tutorial, it is great! I'm fairly new in Linux and I wanted to install and configure arias2 download utility so that it starts up as a resident background program and then a Chromium file-download add-on could send files to it to do the job.
I would never figure out by myself how to start up a program in Linux. Now it seems work. :)
Post Reply

Return to “Tutorials”