Page 1 of 1

Running from cron

Posted: Mon May 24, 2021 9:39 pm
by emeck
Hello,

I have created a simple GUI for use in my RPi. When it opens, a countdown from 10 begins and when reaching 0 it should run the shutdown command:

Code: Select all

Run("/usr/sbin/shutdown", "-h now")
If I launch it from the desktop double clicking on it, everything works fine. But if I lauch it from cron, when it reaches 0 the shutdown won't work. If I replace the command with gimp or lxterminal, for example, it works, both from the desktop or cron, like:

Code: Select all

Run("gimp", "")
I noticed the prompt is different in lxterminal when launched from desktop or cron. Could it be env variables not loaded from cron?

Any Linux guru here that can help me?

My crontat -l:

Code: Select all

30 01 * * * DISPLAY=:0.0 /home/pi/Desktop/shutdowngui
And my script:

Code: Select all

@REQUIRE "RapaGUI", {Link = True}
@DISPLAY {Width = 320, Height = 200, FillStyle = #FILLCOLOR, Color = #GRAY}


timeLeft$ = "10"								;Countdown start

Function p_EventFunc(msg)
	Switch msg.Class
	Case "Button":								;This is the only class for events, but just in case...
		Switch msg.Attribute
		Case "Pressed":
			Switch msg.ID						;Check which button was pressed
			Case "btn_Cancel":					;"Cancel" button was pressed
				End()						;End program and cancel shutdown
			EndSwitch
		EndSwitch
	EndSwitch				
EndFunction

/*******************
 *  Main function  *
 *******************/
Function p_CountDown()
	timeLeft$ = timeLeft$ - 1						;Reduce countdown time
	Cls()										;Clear display
	TextOut(#CENTER, #CENTER, timeLeft$)			;Display new time

	If timeLeft$ = 0							;If countdown ends...
		Run("/usr/sbin/shutdown", "-h now")			;Shutdown system
		End()								;End program
	EndIf
EndFunction


InstallEventHandler({RapaGUI = p_EventFunc})

moai.CreateApp(								;Create GUI from file
[[<?xml version="1.0" encoding="utf-8"?>
<application>
	<window title="ShutdownGUI">
		<vgroup>
			<label align = "Left">Raspberry will shutdown in:</label>
			<hollywood display="1"/>
			<hgroup>
				<rectangle/>
				<button id = "btn_Cancel">Cancel</button>
				<rectangle/>
			</hgroup>
		</vgroup>
	</window>
</application>
]]
)

SetFont(#SANS,56)
SetFontStyle(#ANTIALIAS)
TextOut(#CENTER, #CENTER, timeLeft$)				;Display initial countdown time

SetInterval(1, p_Countdown, 1000)					;Run countdown with 1 second interval

Repeat
	WaitEvent()
Forever

Re: Running from cron

Posted: Sun May 30, 2021 3:51 am
by SamuraiCrow
I noticed the prompt is different in lxterminal when launched from desktop or cron. Could it be env variables not loaded from cron?
It sounds like Cron doesn't use Bash but a raw system call. Does Cron run with root or sudo access? I don't think it does.

Re: Running from cron

Posted: Sun May 30, 2021 5:42 pm
by airsoftsoftwair
Does using Execute() make a difference?

Re: Running from cron

Posted: Sun May 30, 2021 8:39 pm
by emeck
airsoftsoftwair wrote: Sun May 30, 2021 5:42 pm Does using Execute() make a difference?
No, same behavior with Execute(), works double clicking the program, doesn't work from cron.

Re: Running from cron

Posted: Tue Jun 01, 2021 7:42 pm
by airsoftsoftwair
Try starting your program from bash and see if there's any output in the console that might give you a clue about what's going on.