You are not logged in.

#1 2019-06-13 09:56:10

7thCore
Member
Registered: 2018-06-09
Posts: 75

[SOLVED] Crontab not executing a script as user?

So i'm working on a bash script that will manage a game server. While the game server is started and shut down by a custom systemd user service (linger is enabled) and the script does work correctly if i execute it manually, the script dosen't work if it's executed via crontab.

The script checks if the service is active before executing any commands. If it's active it executes the commands, if it's inactive/activating/deactivating/failed, it just exits.

A sample if statment from the script:

SCRIPT_LOG="/path/to/log/file.log"
SERVICE="some.service"

script_autorestart() {
	script_enabled
	if [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" != "active" ]]; then
		echo "$(date +"%Y-%m-%d %H:%M:%S") [$NAME] [INFO] (Autorestart) Server not running, atempting to start." | tee -a  "$SCRIPT_LOG"
		echo "$(systemctl --user is-active $SERVICE)" | tee -a  "$SCRIPT_LOG"
		echo "$(systemctl --user show -p ActiveState --value $SERVICE)" | tee -a  "$SCRIPT_LOG"
		start
		sleep 1
	elif [[ "$(systemctl --user show -p ActiveState --value $SERVICE)" == "active" ]]; then
		echo "$(date +"%Y-%m-%d %H:%M:%S") [$NAME] [INFO] (Autorestart) Server running, no need to restart." | tee -a  "$SCRIPT_LOG"
	fi
}

This is from a log file i have that generates from the script with | tee -a  "$SCRIPT_LOG"
The paranthesis indicate wich function was running.

2019-06-13 11:00:01 [INFO] (Save) Server is not running.
2019-06-13 11:00:01 [INFO] (Sync) Server is not running.
2019-06-13 11:00:01 [INFO] (Autorestart) Server not running, attempting to start.


2019-06-13 11:00:02 [INFO] (Update) Initializing update check.
2019-06-13 11:00:02 [INFO] (Update) Removing Steam/appcache/appinfo.vdf
2019-06-13 11:00:02 [INFO] (Update) Connecting to steam servers.
2019-06-13 11:00:20 [INFO] (Update) Received application info data.
2019-06-13 11:00:20 [INFO] (Update) No new updates detected.
2019-06-13 11:00:20 [INFO] (Update) Installed: BuildID: 3897351, TimeUpdated: 1559900654

If you look at the function there are also two additional lines i added that checks the service. I've added them to see what is going on and both are blank. If i run the script manually both print out: active. At this point i belive the crontab either isn't executing the script as the user it's supposed to or i'm doing something wrong.

My crontab:

0 0,6,12,18 * * * /home/seventhcore/script.bash -save && /home/seventhcore/script.bash -sync && /home/seventhcore/script.bash -update

Any insight on the problem is welcome.

Edit: i know systemd has a restart-on function but i need it to work this way because there are other functions in the script that rely on this method.

Last edited by 7thCore (2019-06-13 19:02:06)


The Linux philosophy is 'Laugh in the face of danger'. Oops. Wrong One. 'Do it yourself'. Yes, that's it.

Offline

#2 2019-06-13 11:10:00

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,423

Re: [SOLVED] Crontab not executing a script as user?

Your crontab likely lacks a lot of the relevant environment to properly access the systemd user session. If you already have such a systemd based workflow, why are you not just using systemd timers?

Offline

#3 2019-06-13 19:01:49

7thCore
Member
Registered: 2018-06-09
Posts: 75

Re: [SOLVED] Crontab not executing a script as user?

Never knew systemd had it's own timer function. Anyhow it works. Thanks!


The Linux philosophy is 'Laugh in the face of danger'. Oops. Wrong One. 'Do it yourself'. Yes, that's it.

Offline

Board footer

Powered by FluxBB