Running from cron

Discuss any general programming issues here
Post Reply
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Running from cron

Post 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
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Running from cron

Post 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.
I'm on registered MorphOS using FlowStudio.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Running from cron

Post by airsoftsoftwair »

Does using Execute() make a difference?
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Re: Running from cron

Post 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.
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Running from cron

Post 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.
Post Reply