Start a service once-off and at a specific time after boot?

Hi, I use plex media server to manage my media. I have it running on my pi2.

It’s set to automatically update my library daily. It updates based on the time that the plexmediaserver service starts.

Problem is that when my pi2 reboots because of a power outage, the time gets messed up and I want it to update at a specific time. Normally I have to manually restart the service at the time I want. How can I automate this process ? However, I can’t just set the service to restart at the specific time in crontab, because I don’t want it to restart every day. I only want it to restart 1 time after the pi2 has turned on (and at a specific time, not immediately).

Any ideas ? Thanks

If I understood correctly, I think the answer for what you are looking for is the at command from the at package as you see here: bash - How to run a script at a certain time on Linux? - Stack Overflow. So just create a startup script or service (probably a systemd service with Type=oneshot) with the command:

echo "systemctl restart SERVICE_NAME"| at DESIRED_TIME

Haven't tested it, so tell me if it works. :stuck_out_tongue:

Hi, could you please explain what you mean by this ? If I were to just run "systemctl restart plexmediaserver" it would need authentication.


I have no idea of raspberry pi but I guess it is the same as a normal installation. So, if that's the case, create a file restart-SERVICE_NAME.service at /lib/systemd/system as this:

Description=Restart SERVICE_NAME at DESIRED_TIME

ExecStart=echo "systemctl restart SERVICE_NAME"| at DESIRED_TIME


Then run:

sudo systemctl daemon-reload
sudo systemctl enable restart-SERVICE_NAME.service

As far as I know the command after ExecStart should be executed as root and work fine (I haven't tested it so tell me what you will find :stuck_out_tongue:). You can maybe do it simpler with a script at /etc/init.d but systemd is the way to do things nowadays. :stuck_out_tongue:

Assuming that raspberry pi makes no difference to what I know, another not-recommended way to do this easier is to create a desktop entry at ~/.config/autostart (or just a startup entry from the mate-session-properties app) with the command:

echo "echo YOUR_PASSWORD| sudo -S systemctl restart YOUR_SERVICE"|at DESIRED_TIME

The main problem here, is that anyone can open the file and see your password so better choose the systemd way (if it works :stuck_out_tongue: ).

Crontab and sleep are your friends here.

add a job into cron and prefix the command with sleep x, where x is the number of seconds you want to wait until you start the service.

@reboot in cron tells it to only run a cmd after boot, not periodically.

@reboot sleep 600; service plexmediaserver restart (or systemctl restart plexmediaserver).

And if you want to get around the password thing, stick it in root or plex user’s crontab, not your own. The service script should hand the running of the server over to another user.

That oughta do it. :slight_smile:

@SFromley I would prefer to not use sleep because then I would have to try and calculate the sleep time based on the current time (because I want it to start at a specific time not after a specific amount time).

@ThanosApostolou Unfortunately using the startup service in systemd/system did not work. I ran the command “atq” after a reboot and it did not show any jobs.

I then went ahead and tried to run the command manually in terminal. It also would not work because of the authentication required for systemctl. The only way I could get it to work was by adding the command: “username NOPASSWD: ALL /bin/systemctl restart plexmediaserver” to the sudoers file so that sudo systemctl could run at the scheduled time. The command then worked fine manually.

Strangely enough, when I input the command (echo “sudo systemctl restart plexmediaserver”|at 16:30) into the startup commands (in the startup applications GUI) it did not work (again no job was scheduled on restart). So instead I added the same command to a bash script and added the bash script to the startup applications. That worked !

I don’t know why the startup service nor the startup command in the GUI did not schedule the job.