You are not logged in.

#1 2014-02-27 16:11:14

gava
Member
Registered: 2014-02-26
Posts: 14

[SOLVED]Get script to run on next boot

tl; dr: What is the best way to execute a script on the next boot (only once)?

The idea is having a hard drive image to be copied to a bunch of pcs and on the first boot they'll register on the server and do some other shananigans (create ssh keys, etc).
The simplest approach would be to create a systemd unit file, enable it for the next boot and have the executed script remove the units symlink on /etc/systemd/system/ but i have the feeling that there should be a better approach.
Some of the pcs are headless, so systemd seems like the best option.
Maybe create a unit file that checks a folder for executables, runs them and deletes them if successful?

Any suggestions on how to improve this are welcome.

Last edited by gava (2014-02-28 18:26:12)

Offline

#2 2014-02-27 16:30:15

andy123
Member
Registered: 2011-11-04
Posts: 169
Website

Re: [SOLVED]Get script to run on next boot

You could do it like the sshdgenkeys.service in the openssh package:

[Unit]
Description=SSH Key Generation
ConditionPathExists=|!/etc/ssh/ssh_host_key
ConditionPathExists=|!/etc/ssh/ssh_host_key.pub
ConditionPathExists=|!/etc/ssh/ssh_host_rsa_key
ConditionPathExists=|!/etc/ssh/ssh_host_rsa_key.pub
ConditionPathExists=|!/etc/ssh/ssh_host_dsa_key
ConditionPathExists=|!/etc/ssh/ssh_host_dsa_key.pub
ConditionPathExists=|!/etc/ssh/ssh_host_ecdsa_key
ConditionPathExists=|!/etc/ssh/ssh_host_ecdsa_key.pub
ConditionPathExists=|!/etc/ssh/ssh_host_ed25519_key
ConditionPathExists=|!/etc/ssh/ssh_host_ed25519_key.pub

[Service]
ExecStart=/usr/bin/ssh-keygen -A
Type=oneshot
RemainAfterExit=yes

What's wrong about letting the script remove the symlink and maybe even the service itself? Sounds like the cleaner solution to me.


i'm sorry for my poor english wirting skills…

Offline

#3 2014-02-27 19:01:41

gava
Member
Registered: 2014-02-26
Posts: 14

Re: [SOLVED]Get script to run on next boot

I didn't know about ConditionPathExists. I like the idea of preventing execution if a file isn't there.

[Unit]
Description=First boot script
ConditionPathExists=/path/to/lock/file

[Service]
ExecStart=/path/to/script
ExecStartPost=/bin/rm /path/to/lock/file
Type=oneshot
RemainAfterExit=yes

This should do.
Just enable the service, then touch /path/to/lock/file when you want the script to be executed on next boot.

There's nothing wrong with letting the script remove the symlink.
I just have a healthy dislike towards scripts that delete files they didn't create or own.
I made a little mess because of a typo in a variable and a rm -r, once... So only if necessary from then on.

Thanks andy123!

Offline

#4 2014-02-27 19:45:25

andy123
Member
Registered: 2011-11-04
Posts: 169
Website

Re: [SOLVED]Get script to run on next boot

Glad I could help. In case you don't seek further support on this issue, I think the forum rules say that you should mark this topic as solved or something like that. (don't quote me on it, but I've seen enough other people ask posters to mark topics as solved…)


i'm sorry for my poor english wirting skills…

Offline

Board footer

Powered by FluxBB