Skip to content

Instantly share code, notes, and snippets.

@matinrco
Last active November 20, 2021 18:30
Show Gist options
  • Save matinrco/7142695d01f7854843f8a509c40e744e to your computer and use it in GitHub Desktop.
Save matinrco/7142695d01f7854843f8a509c40e744e to your computer and use it in GitHub Desktop.
systemd unit template for oneshot execution after boot/sleep/hibernate/hybridsleep

how to use:

  • update yourServiceName in file name & Description in unit file with proper information.
  • add your script to ExecStart. for example:
    ExecStart=/usr/bin/bash -c 'echo "hello world" > /tmp/hello.txt'
    
  • move unit files to /etc/systemd/system directory.
  • apply proper permissions to each file:
    chown root:root /etc/systemd/system/yourServiceName-boot.service
    
    chmod 644 /etc/systemd/system/yourServiceName-boot.service
    
  • run:
    systemctl daemon-reload
    
  • enable each service:
    systemctl enable yourServiceName-boot.service
    
  • start each service:
    systemctl start yourServiceName-boot.service
    
  • ready for action 🚀
[Unit]
Description=your service which do something after boot
After=multi-user.target
StartLimitBurst=0
[Service]
Type=oneshot
Restart=on-failure
ExecStart=your command or script here
[Install]
WantedBy=multi-user.target
[Unit]
Description=your service which do something after hibernation
After=hibernate.target
StartLimitBurst=0
[Service]
Type=oneshot
Restart=on-failure
ExecStart=your command or script here
[Install]
WantedBy=hibernate.target
[Unit]
Description=your service which do something after hybridsleep
After=hybrid-sleep.target
StartLimitBurst=0
[Service]
Type=oneshot
Restart=on-failure
ExecStart=your command or script here
[Install]
WantedBy=hybrid-sleep.target
[Unit]
Description=your service which do something after sleep
After=suspend.target
StartLimitBurst=0
[Service]
Type=oneshot
Restart=on-failure
ExecStart=your command or script here
[Install]
WantedBy=suspend.target
[Unit]
Description=your service which do something after suspendthenhibernate
After=suspend-then-hibernate.target
StartLimitBurst=0
[Service]
Type=oneshot
Restart=on-failure
ExecStart=your command or script here
[Install]
WantedBy=suspend-then-hibernate.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment