You are not logged in.
Hey guys!
After reading several threads and trying several things I still cannot get a custom service to start at boot. I need help.
Here is the story.
I want to delay some services at boot in order to decrease the boot time. So I made this (my first!) script
#!/bin/bash
#This script will run 3 sec after boot
systemctl start dnsmasq.service &&
systemctl start squid.service &&
systemctl start squid-rotate.service
I tried putting the script in /usr/bin and in /usr/local/bin. It runs (manually) fine from either.
After that I created a service
[Unit]
Description=bootscript
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/bootscript
I tried adding
Environment=DISPLAY=:0
as well as changing the type to
Type=simple
After that I created a timer
[Unit]
Description=Runs bootscript at boot
[Timer]
# Time to wait after booting before activation
OnBootSec=3sec
Unit=bootscript.service
[Install]
WantedBy=multi-user.target
Then
systemctl enable bootscript.timer
And after a reboot I have no internet connection. I have to manually run the script in order to start the services.
Script is executable
chmod +x /usr/bin/bootscript
Here is the output
ctl status bootscript.service
● bootscript.service - bootscript
Loaded: loaded (/etc/systemd/system/bootscript.service; static; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2017-11-30 21:33:22 EET; 3s ago
Process: 1120 ExecStart=/usr/bin/bootscript (code=exited, status=203/EXEC)
Main PID: 1120 (code=exited, status=203/EXEC)
Nov 30 21:33:22 ahp systemd[1]: Started bootscript.
Nov 30 21:33:22 ahp systemd[1120]: bootscript.service: Failed to execute command: Exec format error
Nov 30 21:33:22 ahp systemd[1120]: bootscript.service: Failed at step EXEC spawning /usr/bin/bootscript: Exec format error
Nov 30 21:33:22 ahp systemd[1]: bootscript.service: Main process exited, code=exited, status=203/EXEC
Nov 30 21:33:22 ahp systemd[1]: bootscript.service: Failed with result 'exit-code'.
ctl status bootscript.timer
● bootscript.timer - Runs bootscript at boot
Loaded: loaded (/etc/systemd/system/bootscript.timer; enabled; vendor preset: disabled)
Active: active (elapsed) since Thu 2017-11-30 21:27:11 EET; 27min ago
Trigger: n/a
Nov 30 21:27:11 ahp systemd[1]: Started Runs bootscript at boot.
How to proceed to make it work?
Thank you in advance!
Last edited by amaro (2017-12-03 12:10:28)
Offline
I want to delay some services at boot in order to decrease the boot time.
Just stop here. This is completely the wrong approach. Delaying them will not decrease boot time. It may artificially decrease the numbers reported by `systemd-analze` but that does not mean you are getting to a useable system any faster.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
You sank my enthusiasm . I wanted it to work.
Anyway, 'systemd-analyze' showed improvement of 6 sec. But I am not to argue with you. I believe you are right. After all my knowledge is limited.
Now I have
systemctl list-unit-files --state enabled
UNIT FILE STATE
autovt@.service enabled
dhcpcd.service enabled
getty@.service enabled
remote-fs.target enabled
bootscript.timer enabled
'squid' takes about 6 sec to start.
Last edited by amaro (2017-11-30 20:33:10)
Offline
Hi!
You should use the Require and WantedBy to change the order on startup. See systemd-unit for details.
As a general recommendation: Always use absolute paths in units, e.g. /usr/bin/systemctl instead of systemctl.
Offline
Anyway, 'systemd-analyze' showed improvement of 6 sec...
'squid' takes about 6 sec to start.
Well there you have it. With squid starting at boot, systemd-analyze doesn't report booting as "complete" until squid has started - but you will get to your login or desktop just as fast either way. Delaying squid will only delay squid, it will not speed up anything else.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Trilby squid.service is of type forking with WantedBy=multi-user.target so if amaro was using a display manager then
would the display manager only start after multi-user.target is complete which would would require squid.service to finish start up?
Offline
Perhaps, but that then would be the problem to be fixed, and starting important services intentionally after a delay is not a solution for that problem. Change the target they are wanted by, or change the dependency chain for the display manager, or - my favorite - ditch the DM as their primary purposes seem to be to slow down the boot process and break frequently.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Delaying squid will only delay squid, it will not speed up anything else.
Absolutely right. But i did not know it.
Here is what I did in the meantime.
Changed 'Type' to 'forking' as loqs whispered to Trilby
@Trilby squid.service is of type forking with WantedBy=multi-user.target
After that I made two services - one for 'dnsmasq' and one for 'squid'. Hooked them together in that order with a new 'target' file and started the 'target' file with the 'timer'. Found the solution here https://jason.the-graham.com/2013/03/06 … md-timers/
It seems to work cause after rebooting I have access to internet. The only thing I don't get is why the status of both services shows as 'inactive'
ctl status squid
● squid.service - Web Proxy Cache Server
Loaded: loaded (/usr/lib/systemd/system/squid.service; disabled; vendor preset: disabled)
Active: inactive (dead)
ctl status dnsmasq
● dnsmasq.service - A lightweight DHCP and caching DNS server
Loaded: loaded (/usr/lib/systemd/system/dnsmasq.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:dnsmasq(8)
Offline
I finally did it. Created 'target' file that 'Requires' 'dhcpcd', 'dnmasq', 'squid' and 'iptables'. Created 'crontab' for squid-rotate. Started the 'target' with 'timer'. Everything works fine. Overall gain - 3 sec.
Thank you Trilby and loqs! Your comments were very helpful!
Offline