0

I am using the following CoreOS v1688.5.3 [ Rhyolite ] server and i have a specific requirement where i would have to run a particular python script only once, when the server boots up. What would be the best way to achieve this?

Varun
  • 11

2 Answers2

3

The simplest was to achieve this is to create in /etc/crontab next task:

@reboot /path/to/your/python/script.sh

More info you can get from man 5 crontab.

CoreOS doesn't have /etc/crontab.


Another way is to create systemd-timer. Example about systemd-timer you can got from my answer about systemd: Use systemd-shutdownd schedule.

Simple example of systemd-timer which is located at /etc/systemd/system/example.timer:

[Unit]
Description=Run once at system boot

[Timer]
# You may chose one of this triggers
OnBootSec=0min # run after system boot
OnStartupSec=0min # run after systemd was started

[Install]
WantedBy=timers.target # target in wich timer will be installed
  • I guess crontab is not available in coreos :( I like the idea of systemd timers, but does the timer allows me to setup a timer only once, during system start up? – Varun May 24 '18 at 21:09
  • Could i immediately run the script after start up? Like without the 1 min delay? this is because my script tries to change the IP address of server and i want to run it right after the boot. Also is there a way to limit the script to run only once? – Varun May 24 '18 at 21:26
  • Yes, you can. I correct my example. It's right and guaranteed way to run script once. System booted - after last systemd service is started. Systemd booted - after systemd process in memory and it's located at PID 1. – Yurij Goncharuk May 24 '18 at 21:30
1

Using Systemd is the most natural and best way to go:

You need to create a unit for your service in /etc/systemd/system/yourservice.service

[Unit]
Description=your service name

[Service]
Type=oneshot #or simple
ExecStart=/path/to/your/script.py

[Install]
WantedBy=multi-user.target

To enable the service to run on boot you need to run sudo systemctl enable yourservice.service (add --now flag to start the script right away)

You do not give much input but there are a lot of other options which you can use in unit file. Check out man systemd.service and man systemd.unit for more info.

Here is also a link to CoreOS docs:Getting started with systemd