2

I want to reboot the machine after oneshot script is completed and the current service should not be started on next reboot. I have create below systemd unit file

dummy.service

[Unit]
Description=Custom Unit File Automation
Wants=multi-user.target
After=multi-user.target
SuccessAction=reboot

[Service]
User=veeru
Type=oneshot
RemainAfterExit=no
ExecStart=/bin/bash -c "<command> <args>"
StandardOutput=/var/log/command_out.log
StandardError=/var/log/command_err.log
ExecStartPost=/bin/bash -c "/usr/bin/sudo /usr/bin/systemctl disable /etc/systemd/system/playbook_2.service"


[Install]
WantedBy=multi-user.target

I was able to disable the service systemctl. But not able to reboot the machine, in the status, I see

Jun 28 01:45:55 hyddrogen systemd[1]: [/etc/systemd/system/dummy.service:5] Unknown lvalue 'SuccessAction' in section 'Unit'

Is there any proper options to disable service and reboot machine. I can put reboot in ExecStartPost, but I don't want to do that

UPDATE

I got SuccessAction option from this doc

3
  • You got that error because "In user mode, only none, exit, and exit-force are allowed"
    – Jeff Schaller
    Jun 27 '19 at 15:33
  • Right, now I see that in doc. Can you suggest any other options? Jun 27 '19 at 15:55
  • 1
    I don't have an Answer; I just wanted to point out why your one option wasn't working.
    – Jeff Schaller
    Jun 27 '19 at 15:56
1

After some digging, I found a way to do this. Here is my unit file

[Unit]
Description=Custom Unit File Automation
After=default.target
DefaultDependencies=no
Before=shutdown.target

[Service]
User=veeru
Type=oneshot
RemainAfterExit=no
ExecStart=/bin/bash -c "<command> <args>"
StandardOutput=file:/var/log/command_out.log
StandardError=file:/var/log/command_err.log
ExecStartPost=/usr/bin/sudo /usr/bin/systemctl disable /etc/systemd/system/dummy.service
ExecStop=/usr/bin/sudo /usr/sbin/reboot

[Install]
WantedBy=default.target

From systemd "oneshot" service to trigger poweroff , I have added DefaultDependencies=no and Before=shutdown.target, slightly better way to stop daemon before reboot

0

That solution seems a bit hacky. I'm having trouble understanding why you would want a oneshot service that calls reboot, why not just use a script?

Anyhow, if you really wanted to do this as a systemd unit, you could simply have the oneshot touch a file, and then have a conditional that checks for the file.

Such as:

[Unit]
ConditionPathExists=/var/Custom_command.data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.