[SOLVED] Script shutting down

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
macbeto
Level 3
Level 3
Posts: 134
Joined: Tue Jan 29, 2019 5:04 am

[SOLVED] Script shutting down

Post by macbeto »

Hi,

I've got in a script the command cinnamon-session-quit. When I execute the script it launch the shutting down window which is the expected.
But when I execute the script from the crontab it doesnt work and doesnt do anything.
I am trying to shutdown the PC at some hours at night to avoid remaining on during the night but I dont find the way except executing the shutdown command, which works but it shutdown without asking first if you wanna continuing connected.
Can anybody advice me what is wrong with this command in crontab?
Thanks.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script shutting down

Post by rene »

The trouble is cron jobs don't run in the context of your graphical environment; specifically, have no idea about the for cinnamon-session-quit necessary session bus address nor, in fact, even which X server to contact. The best answer to the question is using a systemd user timer rather than cron but unfortunately Ubuntu/Mint doesn't at the moment yet make that easy, them not using the user graphical.target.

This is to say that the way to currently wing it is to just provide the needed information to cinnamon-session-quit manually: replace the cinnamon-session-quit command with

Code: Select all

env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus DISPLAY=:0  cinnamon-session-quit
That specific DBUS_SESSION_BUS_ADDRESS assumes Mint 19; the DISPLAY value will be correct unless you already know it to not be.
macbeto
Level 3
Level 3
Posts: 134
Joined: Tue Jan 29, 2019 5:04 am

Re: Script shutting down

Post by macbeto »

First of all thank you very much for take the time to answer my question.
I didn't know cron wasn't related with the graphical side of the Linux
My OS is Linux Mint 19.1.
I just tested your code in a cron job but for me unfortunatelly it doesn't work and doesn't show the shut down window. Executing that code manually works ok, but again my cron job execute the rest of the steps but doesn't work with what this code intents to do.
Regard.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script shutting down

Post by rene »

I've just tested and it works fine for me on 19.1 Cinnamon; shall as such for now have to assume something wromg with your implementation of the suggestion. I.e., can you paste back your cron line and/or from it called script, redacted as little as possible?
macbeto
Level 3
Level 3
Posts: 134
Joined: Tue Jan 29, 2019 5:04 am

Re: Script shutting down

Post by macbeto »

My cron line is:
* * * * * sh /folder/scriptname.sh
So it has to run every minute.
Then the script is just your code and then I added "touch /folder/test.txt" to make sure the cron job execute the script, and it creates the test.txt file but doesn't show the shut down window.
I also prefixed dbus-launch to cinnamon-session-quit to the code and it shows all the enviroment variables with the DBUS_SESSION_BUS_ADDRESS and DISPLAY as introduced but the cinnamon-session-quit is failing because the name org.gnome.sessionmanager was not provided by any .service files (that is the message it returns).
It is weird that it's working for you, I dont think I have anything special in my computer.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script shutting down

Post by rene »

The error message you get signifies inability to communicate over the session bus, which is what a bad or missing DBUS_SESSION_BUS_ADDRESS variable would get you. Since I would know of no reason you'd on 19.1 not have the /run/user/$(id -u)/bus socket the still only thing I can suspect is typo; that's why I requested paste rather than description -- although (edit) the "dbus-launch" clearly does foul things up, by e.g. launching a NEW bus. I was assuming you first tested without; please do so from a new boot now if not.

I've by now tested that this works fine both for administrator and standard users on 19.1 Cinnamon and with or without the additional polkit permission in place I normally have. My crontab line:

Code: Select all

rene@t5500:~$ crontab -l
# Edit this file to introduce tasks to be run by cron.
[ ... ]
# m h  dom mon dow   command
* * * * * env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus DISPLAY=:0 cinnamon-session-quit --power-off
This gets me a shutdown dialogue every minute (which is of course wildly annoying but I suppose you're testing).

Note for onlookers/testers, "DISPLAY=:0" needs to be tweaked to e.g. DISPLAY=:1 for a second logged in user but given the message OP gets that's not the issue; here the session bus is not found.

If definitively no typo, please see if you actually have a /run/user/$(id -u)/bus socket.
macbeto
Level 3
Level 3
Posts: 134
Joined: Tue Jan 29, 2019 5:04 am

Re: Script shutting down

Post by macbeto »

Hi Rene, I really appreciate the time you are taking to answer this post and your help.
For me it is still not working.
The command echo $(id -u) return 1000, and in the path /run/user/1000 I got this file (ls -la):

Code: Select all

srw-rw-rw-  1 linux linux   0 mar 10 08:15 bus 

Also I tryed with DISPLAY 0,1 and 2 and neither works.
My command sudo crontab -e looks like this:

Code: Select all

# APAGADO DE MÁQUINA DE DOMINGO A JUEVES A LAS 00:45
45 00 * * 1,2,3,4,5 /sbin/shutdown -h
# APAGADO DE MÁQUINA VIERNES Y SABADO A LAS 01:15
15 01 * * 6,7 /sbin/shutdown -h
* * * * * env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus DISPLAY=:0 cinnamon-session-quit
The previous lines are working but as I told it shutdowns the system without any warning, I assume because shutdown is not a graphical command and crontab is in the same context.
The env looks like this:

Code: Select all

LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
LC_MEASUREMENT=es_ES.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
LC_PAPER=es_ES.UTF-8
LC_MONETARY=es_ES.UTF-8
LANG=es_ES.UTF-8
GDM_LANG=es_ES
DISPLAY=:0
GTK_OVERLAY_SCROLLING=0
COLORTERM=truecolor
DESKTOP_AUTOSTART_ID=1085a2ba6df6e5463d155220215792293800000012230030
XDG_VTNR=7
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
MANDATORY_PATH=/usr/share/gconf/cinnamon.mandatory.path
LC_NAME=es_ES.UTF-8
XDG_SESSION_ID=c1
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/linux
USER=linux
DESKTOP_SESSION=cinnamon
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/35ad9eb5_25fb_4bc6_bfcc_1e576d9cb82f
DEFAULTS_PATH=/usr/share/gconf/cinnamon.default.path
QT_QPA_PLATFORMTHEME=qt5ct
PWD=/run/user/1000
HOME=/home/linux
SSH_AGENT_PID=1290
QT_ACCESSIBILITY=1
XDG_SESSION_TYPE=x11
XDG_DATA_DIRS=/usr/share/cinnamon:/usr/share/gnome:/home/linux/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
XDG_SESSION_DESKTOP=cinnamon
LC_ADDRESS=es_ES.UTF-8
LC_NUMERIC=es_ES.UTF-8
GTK_MODULES=gail:atk-bridge
TERM=xterm-256color
SHELL=/bin/bash
VTE_VERSION=5202
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_CURRENT_DESKTOP=X-Cinnamon
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
GNOME_TERMINAL_SERVICE=:1.454
XDG_SEAT=seat0
SHLVL=1
LANGUAGE=es_ES:es
LC_TELEPHONE=es_ES.UTF-8
GDMSESSION=cinnamon
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LOGNAME=linux
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
XAUTHORITY=/home/linux/.Xauthority
XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0
XDG_CONFIG_DIRS=/etc/xdg/xdg-cinnamon:/etc/xdg
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
LC_IDENTIFICATION=es_ES.UTF-8
SESSION_MANAGER=local/linux:@/tmp/.ICE-unix/1223,unix/linux:/tmp/.ICE-unix/1223
LESSOPEN=| /usr/bin/lesspipe %s
LC_TIME=es_ES.UTF-8
_=/usr/bin/env
OLDPWD=/home/linux
I have only one user which is the administrator.
Many thanks for your help.
macbeto
Level 3
Level 3
Posts: 134
Joined: Tue Jan 29, 2019 5:04 am

Re: Script shutting down

Post by macbeto »

In the losg syslog it shows the execution of the cron job which it looks pretty normal to me discarting the output:

Code: Select all

Mar 10 09:20:01 Linux CRON[5721]: (root) CMD (env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus DISPLAY=:0 cinnamon-session-quit)
Mar 10 09:20:01 Linux CRON[5720]: (CRON) info (No MTA installed, discarding output)
macbeto
Level 3
Level 3
Posts: 134
Joined: Tue Jan 29, 2019 5:04 am

Re: Script shutting down

Post by macbeto »

Furthermore inside my Linux Mint in virtualbox I have a Linux Mint machine just with the default and clean installation of R.19.1 and creating a cron job with your code also do nothing for me.
I am pretty sure must be something that I am doing wrong but I don't figure it out so far.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Script shutting down

Post by rene »

macbeto wrote: Sun Mar 10, 2019 4:52 am My command sudo crontab -e looks like [ ... ]
Ah. You are putting this in root's crontab. User 'root' is not logged in, does not have a session bus nor even a graphical environment up. Seeing as how the goal here is to get your own user's graphical environment to put up a dialogue, put it in your own crontab. I.e., as that user, crontab -e to edit, crontab -l to list and so on; no sudo.

I take it you started to use root's crontab due to needing it for the plain "shutdown" cases; that issue can be dealt with by using root's crontab (i.e., no user-specific resource such as the logged-in-to graphical environment to access) but should/can preferably be by in fact giving your user or all administrator users enough permission to also shutdown from cron. How has been posted here fairly frequently; see e.g. here for the probably most recent one: viewtopic.php?f=213&t=288631#p1599017

But that's not the point then. In this case, you want cron to provide you access to user-specific resources, a session bus and an X display, so use your user's crontab for this.
macbeto
Level 3
Level 3
Posts: 134
Joined: Tue Jan 29, 2019 5:04 am

Re: Script shutting down

Post by macbeto »

Rene, you are totally right.
I didn't know there was different crontabs based in the users. I need to learn quite a lot about Linux, I am newby in all this, just some months with Linux Mint, but it is absolutelly fascinating.
Now it works perfectly well and just what I needed.
Thank you very much for your help, knowledge and time spent.
Regards
Locked

Return to “Scripts & Bash”