How to change systemd service timeout value?
My systemd service kept timing out because of how long it would take to boot up also, so this fixed it for me:
- Edit your systemd file:
- For modern versions of
systemd
: Runsystemctl edit --full node.service
(replace "node" with your service name).- This will create a system file at
/etc/systemd/system/node.service.d/
that will override the system file at/usr/lib/systemd/system/node.service
. This is the proper way to configure your system files. More information about how to usesystemctl edit
is here.
- This will create a system file at
- Directly editing system file: The system file for me is at
/usr/lib/systemd/system/node.service
. Replace "node" with your application name. However, it is not safe to directly edit files in/usr/lib/systemd/
(See comments)
- For modern versions of
Use
TimeoutStartSec
,TimeoutStopSec
orTimeoutSec
(more info here) to specify how long the timeout should be for starting & stopping the process. Afterwards, this is how my systemd file looked:[Unit] Description=MyProject Documentation=man:node(1) After=rc-local.service [Service] WorkingDirectory=/home/myproject/GUIServer/Server/ Environment="NODE_PATH=/usr/lib/node_modules" ExecStart=-/usr/bin/node Index.js Type=simple Restart=always KillMode=process TimeoutSec=900 [Install] WantedBy=multi-user.target
- You can also view the current Timeout status by running any of these (but you'll need to edit your service to make changes! See step 1). Confusingly, the associated properties have a "U" in their name for microseconds. See this Github issue for more information:
systemctl show node.service -p TimeoutStartUSec
systemctl show node.service -p TimeoutStopUSec
systemctl show node.service -p TimeoutUSec
- You can also view the current Timeout status by running any of these (but you'll need to edit your service to make changes! See step 1). Confusingly, the associated properties have a "U" in their name for microseconds. See this Github issue for more information:
Next you'll need to reload the systemd with
systemctl reload node.service
- Now try to start your service with
systemctl start node.service
- If that didn't work, try to reboot systemctl with
systemctl reboot
- If that didn't work, try using the
--no-block
option for systemctl like so:systemctl --no-block start node.service
. This option is described here: "Do not synchronously wait for the requested operation to finish. If this is not specified, the job will be verified, enqueued and systemctl will wait until the unit's start-up is completed. By passing this argument, it is only verified and enqueued."- There is also the option to use
systemctl mask
instead ofsystemctl start
. For more info see here.
- There is also the option to use
Updates from Comments:
TimeoutSec=infinity
: Instead of using "infinity" here, put a large amount of time instead, likeTimeoutSec=900
(15 min). If the application takes "forever" to exit, then it's possible that it will block a reboot indefinitely. Credit @Alexis Wilke and @JCCyC- Instead of editing
/usr/lib/systemd/system
, trysystemctl edit
instead or edit/etc/systemd/system
to override them instead. You should never edit service files in/usr/lib/
. Credit @ryeager and @0xC0000022L
Running systemctl show SERVICE_NAME.service -p TimeoutStopUSec
I could at least see the timeout set by systemd to my service.
I changed the script to a regular unit file one in order for it work properly.