You are not logged in.

#1 2013-01-09 23:11:30

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

User crontab execute script only if user is logged into X [SOLVED]

How can I added a check to see if the user is logged in graphically to a crontab line?  I do not want the script cron is targeting to run unless the user is logged into X.  Google has been surprisingly unhelpful.

Thanks all.

Last edited by graysky (2013-01-09 23:28:10)

Offline

#2 2013-01-09 23:26:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,554
Website

Re: User crontab execute script only if user is logged into X [SOLVED]

I feel like I might be missing part of the problem ... wouldn't this work:

[[ -n $DISPLAY ]] && /path/to/something/to/do

EDIT: ah, I suppose I am, the DISPLAY variable is only shared within a session.

Last edited by Trilby (2013-01-09 23:28:14)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2013-01-09 23:28:01

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Re: User crontab execute script only if user is logged into X [SOLVED]

Stellar!  I think that will work.  Thx.

Offline

#4 2013-01-09 23:29:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,554
Website

Re: User crontab execute script only if user is logged into X [SOLVED]

I edited while you were posting.  I hope that could work, but it depends on how the user crontab is running.

If (when) that fails, you could look into parsing the output of `ps` with flags to get just the appropriate user's processes and check for xinit or something similar.

EDIT: this should do

[[ -n "$(ps -eo args | grep -E "^X")" ]] && /something/to/do

but then this is much simpler if you have pgrep installed (which I sppose you have to if you have ps)

pgrep -c X >/dev/null && /something/to/do

Last edited by Trilby (2013-01-09 23:43:28)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2013-01-10 20:24:42

teateawhy
Member
From: GER
Registered: 2012-03-05
Posts: 1,138
Website

Re: User crontab execute script only if user is logged into X [SOLVED]

Another approch to this is systemd. The following command returns -3 if X is not active, and 0 otherwise.

systemctl is-active graphical.target --quiet

However the safer method is pgrep, because X can be started while graphical.target is not active.

Last edited by teateawhy (2013-01-10 20:25:03)

Offline

Board footer

Powered by FluxBB