cronsystemctlubuntu-22.04

Crontab failing to restart systemctl process


Hi im trying to setup a cron job to restart a game server daily but its failing to work

i have used crontab -e to install a job

* 3 * * * systemctl --user restart <servicename> this failed to restart my process at 3 am as expected.

i also tried moving the command to a bash file and running the bash file through cron (as i have seen alot of people use .sh files to run commands)

additionally i have tried installing to job as root with zero success

this had similar results

my syslog did show a command execute as expected yesterday though the service didnt restart, since then cron isnt showing any new attempts.

i found some info on troubleshooting and following the guidance shows that my cron is working, i managed write the date/time to a file every minute and the service is clearly running and this showed in the logs.

is there a problem with cron running systemctl commands or is there further steps i can use to see why its failing?

Edit: i tried running my command as root also and nothing happened

Jan 31 09:00:01 ns509515 CRON[108307]: (root) CMD (<user> systemctl --user restart EcoServer)
Jan 31 09:01:01 ns509515 CRON[108330]: (root) CMD (<user> systemctl --user restart EcoServer)
Jan 31 09:02:01 ns509515 CRON[108356]: (root) CMD (<user> systemctl --user restart EcoServer)

Solution

  • In the End this was achieved using a systemd timer service.

    // %h/.config/systemd/user/EcoRestart.service
    [Unit]
    Description=Restart Eco Server daily
    
    [Service]
    Type=oneshot
    ExecStart=/bin/systemctl --user restart EcoServer.service
    
    [Install]
    WantedBy=default.target
    
    
    // %h/.config/systemd/user/EcoRestart.timer
    [Timer]
    OnCalendar=*-*-* 08:00:00
    
    [Install]
    WantedBy=timers.target
    

    probably not the best or most efficient way, but it serves it's purpose.