Running desktop scripts from cron [SOLVED]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Running desktop scripts from cron [SOLVED]

Post by Logansfury »

Hello,

I have been following tutorials all night trying to get my cron to execute a .sh script. I have made simple scripts such as:

#!/bin/bash
# Play a beep sound at max volume paplay /usr/share/sounds/freedesktop/stereo/complete.oga

and

#!/bin/bash
notify-send "Alert"

I am using crontab -e in terminal to edit my cron, then using time commands of 1 to 2 minutes into the future to test ie:

25 21 * * * /home/logansfury/xdotool/beep.sh

no matter what I have tried following Linux tutorials or inquiring at chatgpt3.5 I cannot get a solution. Does anyone know why my cron doesn't work?

Thanks for reading,

Logan
Last edited by Logansfury on Tue Nov 07, 2023 10:46 pm, edited 2 times in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Cron is broken on my new Linux box, please help!

Post by xenopeek »

cron is intended for system services and user automation (like backups). Scripts run from cron don't have access to your desktop or dbus, so they can't interface with PulseAudio or the window manager. For the latter you can try prefixing the script with env DISPLAY=:0 to give it access to your window manager. For PulseAudio something else is probably needed, maybe setting the DBUS_SESSION_BUS_ADDRESS. It gets hacky real quick.

Maybe if you look in your journal you'll see errors. As a general rule you should always capture the output of scripts that you run unattended and print when they start and stop. To capture output postfix your script with something like >> /home/username/script.log 2>&1 and in your script put something like:

Code: Select all

#!/bin/bash
echo "[$$] Start at $(date)"
# your code here
echo "[$$] Finish at $(date)"
But it's probably better to start the script from Startup Applications and schedule itself when to run. That way it runs in your user session and has access to your desktop and dbus. What are you actually trying to do?
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Cron is broken on my new Linux box, please help!

Post by Logansfury »

Hello, thank you for the reply.

Where do I use "env DISPLAY=:0" do I edit that into the .sh before the notification code?

Rather than a one time on one day, could you please edit the code for daily execution?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Cron is broken on my new Linux box, please help!

Post by xenopeek »

You add it on the crontab line from where you run the script, not in the script. For the second search for "crontab generator" and use one of those websites to generate the schedule.

admin note: You made 2 other topics about the same thing. Cross posting is not allowed. I've removed the other topics about the same.
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Cron is broken on my new Linux box, please help!

Post by Logansfury »

Thank you for the continued help. I have installed bcron but it doesn't seem to have done anything but lock down cron.

crontab -e now gives me a message not to edit the file but instead edit master at /etc/crontab

I have edited that file:

Code: Select all

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

14 * * * *  root run-parts --report /etc/cron.hourly
24 4 * * *  root test -x /usr/sbin/anacron || run-parts --report /etc/cron.daily
39 4 * * 7  root test -x /usr/sbin/anacron || run-parts --report /etc/cron.week>
54 4 1 * *  root test -x /usr/sbin/anacron || run-parts --report /etc/cron.mont>
env DISPLAY=:0
23 1 * * *  /home/logansfury/notify.sh
However when 1:23 came along, the script was never launched.

What should I do now?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Cron is broken on my new Linux box, please help!

Post by AndyMH »

You do not put DISPLAY=:0 in crontab, you put it in your script. cron jobs run in a completely different environment to the user and this trips people up when they try and use it for the first time, it did me.

I have a script rsyncing home to my NAS once a week, towards the top of the script it has this:

Code: Select all

#this is needed for notify-send to work
export XDG_RUNTIME_DIR=/run/user/$(id -u) 
#this is needed for yad to work
export DISPLAY=:0 

yad is used for a simple GUI when rsync is running and on completion notify-send sends me a notification.

The best way to check that cron is doing what it is supposed to is to put something like this in your script for testing

Code: Select all

echo "my cronjob has run" > /home/me/somefile
Check somefile to determine if it ran, this does not depend on getting the environment right. You can also tart it up a bit and echo current date/time to the file. Once you know cron is running it you can remove the lines from your script and concentrate on the rest.

What happens if your PC is not on when the cronjob is supposed to run? Answer - the job will not get run, is that an issue?
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Cron is broken on my new Linux box, please help!

Post by Logansfury »

Hello,

You posted just before I could edit the title for SOLVED :D

A user helped me out and discovered my permissions were fried. As soon as I did a chown command I got my crontab -e command back, got into the correct crontab, and was able to use scripted workarounds to enable running both audio .wav files, and notifications!

I'm set. Here is my working notify.sh for anyone interested:

#!/bin/bash

export XDG_RUNTIME_DIR=/run/user/1000
notify-send -i dialog-warning "ALERT!"
# Play a beep sound at max volume
paplay /usr/share/sounds/freedesktop/stereo/complete.oga

and the crontab entry:

05 03 * * * env DISPLAY=:1 /home/logansfury/xdotool/notify.sh
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

This one may be complicated. Using crontab to launch a bash .sh script with python3 coding.

Post by Logansfury »

Hello everyone,

I have been working with xdotool, bash .sh scripts, and crontab to launch small scripted events at certain times.

I have bash scripts that display Notifications while using paplay to play a .wav file. Crontab is opening these scripts on schedules just fine with the following entry:

20 01 * * * env DISPLAY=:1 /home/logansfury/xdotool/random.sh

Direct commands to play sounds w/paplay in crontab worked as follows:

19 16 * * * export XDG_RUNTIME_DIR=/run/user/1000 && paplay /home/logansfury/Documents/sound101.wav

Now comes the problem. I got an amazing bash .sh script composed by ChatGPT3.5. It used paplay to play a random .wav and uses python3 to temporarily change the desktop background wallpaper, then after the .wav file finishes playing, it reverts the wallpaper back.

I cannot get this script to run with crontab. I can test it in terminal and it runs fine, I have done the chmod command on it. I even tried making a simple bash script to launch the script with python code, and tried having crontab open that, but it would not launch the bash with python code. It WOULD launch simple paplay scripts.

What please is necessary to get my .sh bash script with python3 code contained to be launched on schedule with crontab?

My system info:

Code: Select all

System:
  Kernel: 5.15.0-88-generic x86_64 bits: 64 compiler: gcc v: 11.4.0 Desktop: Cinnamon 5.8.4
    tk: GTK 3.24.33 wm: muffin dm: LightDM Distro: Linux Mint 21.2 Victoria base: Ubuntu 22.04 jammy
Machine:
  Type: Desktop System: Dell product: OptiPlex 5040 v: N/A serial: <superuser required> Chassis:
    type: 3 serial: <superuser required>
  Mobo: Dell model: 0T7D40 v: A01 serial: <superuser required> UEFI: Dell v: 1.21.0
    date: 12/08/2021
CPU:
  Info: quad core model: Intel Core i5-6500 bits: 64 type: MCP arch: Skylake-S rev: 3 cache:
    L1: 256 KiB L2: 1024 KiB L3: 6 MiB
  Speed (MHz): avg: 3401 high: 3512 min/max: 800/3600 cores: 1: 3358 2: 3380 3: 3357 4: 3512
    bogomips: 25599
  Flags: avx avx2 ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx
Graphics:
  Device-1: AMD Turks PRO [Radeon HD 6570/7570/8550 / R5 230] driver: radeon v: kernel pcie:
    speed: 5 GT/s lanes: 16 ports: active: HDMI-A-1,HDMI-A-2 empty: VGA-1 bus-ID: 01:00.0
    chip-ID: 1002:6759
  Display: x11 server: X.Org v: 1.21.1.4 driver: X: loaded: ati,radeon
    unloaded: fbdev,modesetting,vesa gpu: radeon display-ID: :0 screens: 1
  Screen-1: 0 s-res: 3840x1080 s-dpi: 96
  Monitor-1: HDMI-0 mapped: HDMI-A-1 pos: primary,left model: Asus VS278 res: 1920x1080 dpi: 82
    diag: 686mm (27")
  Monitor-2: HDMI-1 mapped: HDMI-A-2 pos: right model: VG27A res: 1920x1080 dpi: 82
    diag: 685mm (27")
  OpenGL: renderer: AMD TURKS (DRM 2.50.0 / 5.15.0-88-generic LLVM 15.0.7)
    v: 4.5 Mesa 23.0.4-0ubuntu1~22.04.1 direct render: Yes
Audio:
  Device-1: Intel 100 Series/C230 Series Family HD Audio vendor: Dell driver: snd_hda_intel
    v: kernel bus-ID: 00:1f.3 chip-ID: 8086:a170
  Device-2: AMD Turks HDMI Audio [Radeon HD 6500/6600 / 6700M Series] driver: snd_hda_intel
    v: kernel pcie: speed: 5 GT/s lanes: 16 bus-ID: 01:00.1 chip-ID: 1002:aa90
  Device-3: Generalplus USB Audio Device type: USB driver: hid-generic,snd-usb-audio,usbhid
    bus-ID: 1-5.2:41 chip-ID: 1b3f:2008
  Sound Server-1: ALSA v: k5.15.0-88-generic running: yes
  Sound Server-2: PulseAudio v: 15.99.1 running: yes
  Sound Server-3: PipeWire v: 0.3.48 running: yes
Network:
  Device-1: Intel Ethernet I219-V vendor: Dell driver: e1000e v: kernel port: N/A bus-ID: 00:1f.6
    chip-ID: 8086:15b8
  IF: enp0s31f6 state: down mac: <filter>
  Device-2: Realtek RTL8188ETV Wireless LAN 802.11n Network Adapter type: USB driver: r8188eu
    bus-ID: 1-1:2 chip-ID: 0bda:0179
  IF: wlx7cb232bdeee1 state: up mac: <filter>
Drives:
  Local Storage: total: 465.76 GiB used: 50.69 GiB (10.9%)
  ID-1: /dev/sda vendor: Seagate model: ST500DM009-2F110A size: 465.76 GiB speed: 6.0 Gb/s
    serial: <filter>
Partition:
  ID-1: / size: 456.89 GiB used: 50.69 GiB (11.1%) fs: ext4 dev: /dev/sda2
  ID-2: /boot/efi size: 511 MiB used: 6.1 MiB (1.2%) fs: vfat dev: /dev/sda1
Swap:
  ID-1: swap-1 type: file size: 2 GiB used: 0 KiB (0.0%) priority: -2 file: /swapfile
USB:
  Hub-1: 1-0:1 info: Hi-speed hub with single TT ports: 16 rev: 2.0 speed: 480 Mb/s
    chip-ID: 1d6b:0002
  Device-1: 1-1:2 info: Realtek RTL8188ETV Wireless LAN 802.11n Network Adapter type: Network
    driver: r8188eu rev: 2.0 speed: 480 Mb/s chip-ID: 0bda:0179
  Hub-2: 1-5:3 info: Terminus USB 2.0 Hub ports: 4 rev: 2.0 speed: 480 Mb/s chip-ID: 1a40:0801
  Hub-3: 1-5.1:40 info: Terminus USB 2.0 Hub ports: 4 rev: 2.0 speed: 480 Mb/s
    chip-ID: 1a40:0801
  Device-1: 1-5.2:41 info: Generalplus USB Audio Device type: Audio,HID
    driver: hid-generic,snd-usb-audio,usbhid rev: 1.1 speed: 12 Mb/s chip-ID: 1b3f:2008
  Hub-4: 1-5.4:6 info: ASIX AX68004 ports: 7 rev: 1.0 speed: 12 Mb/s chip-ID: 0b95:6804
  Device-1: 1-5.4.1:7 info: Primax USB Optical Mouse type: Mouse driver: hid-generic,usbhid
    rev: 2.0 speed: 12 Mb/s chip-ID: 0461:4de2
  Device-2: 1-5.4.2:8 info: Logitech Keyboard K120 type: Keyboard,HID driver: hid-generic,usbhid
    rev: 1.1 speed: 12 Mb/s chip-ID: 046d:c31c
  Device-3: 1-5.4.6:9 info: ASIX AX68004 type: Keyboard,Mouse driver: hid-generic,usbhid
    rev: 1.1 speed: 12 Mb/s chip-ID: 2224:ff2c
  Hub-5: 2-0:1 info: Super-speed hub ports: 10 rev: 3.0 speed: 5 Gb/s chip-ID: 1d6b:0003
Sensors:
  System Temperatures: cpu: 29.8 C pch: 49.5 C mobo: 27.8 C gpu: radeon temp: 55.0 C
  Fan Speeds (RPM): N/A
Repos:
  Packages: 2468 apt: 2452 flatpak: 16
  No active apt repos in: /etc/apt/sources.list
  Active apt repos in: /etc/apt/sources.list.d/bitseater-ppa-jammy.list
    1: deb [signed-by=/etc/apt/keyrings/bitseater-ppa-jammy.gpg] https: //ppa.launchpadcontent.net/bitseater/ppa/ubuntu jammy main
  Active apt repos in: /etc/apt/sources.list.d/official-package-repositories.list
    1: deb http: //packages.linuxmint.com victoria main upstream import backport
    2: deb http: //archive.ubuntu.com/ubuntu jammy main restricted universe multiverse
    3: deb http: //archive.ubuntu.com/ubuntu jammy-updates main restricted universe multiverse
    4: deb http: //archive.ubuntu.com/ubuntu jammy-backports main restricted universe multiverse
    5: deb http: //security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
  Active apt repos in: /etc/apt/sources.list.d/syncthing.list
    1: deb [signed-by=/usr/share/keyrings/syncthing-archive-keyring.gpg] https: //apt.syncthing.net/ syncthing candidate
  Active apt repos in: /etc/apt/sources.list.d/teejee2008-foss-jammy.list
    1: deb [signed-by=/etc/apt/keyrings/teejee2008-foss-jammy.gpg] https: //ppa.launchpadcontent.net/teejee2008/foss/ubuntu jammy main
Info:
  Processes: 302 Uptime: 2d 8h 36m Memory: 31.27 GiB used: 3.09 GiB (9.9%) Init: systemd v: 249
  runlevel: 5 Compilers: gcc: 11.4.0 alt: 11/12 Client: Unknown python3.10 client inxi: 3.3.13

Thank you for reading,

Logan
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Hoser Rob
Level 20
Level 20
Posts: 11796
Joined: Sat Dec 15, 2012 8:57 am

Re: This one may be complicated. Using crontab to launch a bash .sh script with python3 coding.

Post by Hoser Rob »

Logansfury wrote: Tue Nov 07, 2023 9:50 am ... I got an amazing bash .sh script composed by ChatGPT3.5. It used paplay to play a random .wav and uses python3 to temporarily change the desktop background wallpaper, then after the .wav file finishes playing, it reverts the wallpaper back....
I wouldn't touch an AI to do this and certainly wouldn't assume it'd work properly.
For every complex problem there is an answer that is clear, simple, and wrong - H. L. Mencken
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: This one may be complicated. Using crontab to launch a bash .sh script with python3 coding.

Post by Logansfury »

It works flawlessly. It took some patience to coax what I wanted from the bot but it delivered. It is just script to randomly select a folder, then use python3 to change the wallpaper to the .png in that folder and paplay the play the .wav in that folder. It isnt anything I could have possibly composed myself.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Addressing problem from different angle: How to run a python3 .py script from crontab?

Post by Logansfury »

Hello,

I hope this isn't considered a cross-post. I want to address my problem a different way and cant seem to delete the prior post.

I am having an issue getting some scripts running in crontab. I was working with a xdotool bash scripts and have successfully scheduled .sh scripts to run that include playing a .wav file and displaying a Notification.

I had a fun idea for a more complicated bash script and ChatGPT3.5 delivered a working .sh script that includes python3 commands. The script WORKS. I have performed the chmod command and run it from both double-click and selecting terminal, and by typing the ./script.sh command in terminal. However I could not get it to launch in crontab. I took some steps back to try and just get a start. I had the bot create 2 simple .py's one to play a .ogg system sound, and the other to change wallpaper. Both work independently. However the bot instructed to put into the crontab:

30 15 * * * /usr/bin/python3 /path/to/your/script.py

This failed to launch the simple scripts.

IS is possible to even launch a py script from crontab? If so what is the correct code to replace the /usr/bin/python3 bit with?

Thank you very much for reading,

Logan
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Cron is broken on my new Linux box, please help! [SOLVED]

Post by xenopeek »

Merged your 3 topics because they are about the same thing. If you want to use cron to run non-system tasks — tasks that need access to the window system, dbus etc — you need to configure the environment for those task accordingly. As you did for the bash script. Either with env on the cron line or by modifying the environment from the task itself, you have to do the same thing for tasks written in other programming languages. And make your scripts write a log file so you can do your own troubleshooting why the script aborts.
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Cron is broken on my new Linux box, please help!

Post by Logansfury »

Thank you very much for combining the posts, I wanted to delete one and start over.

The bot actually touched on this!

"Environment Variables: If your script relies on environment variables, set them explicitly in the cron job using the env command. For example:

bash
Copy code
0 8 * * * env VAR_NAME=value /usr/bin/python3 /path/to/your/change_wallpaper.py"

The problem is I am brand new, know nothing about computer environments and have no idea how to edit this example to the correct code.

The bot also just gave me the code for troubleshooting log:

">> /path/to/your/cron_log.log 2>&1"

I am going to amend the crontab with this log
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Cron is broken on my new Linux box, please help! [SOLVED]

Post by Logansfury »

OK I did the edit of:

16 10 * * * /usr/bin/python3 /home/logansfury/python_work/beep.py >> /home/logansfury/Desktop/cron_log.log 2>&1

the .py file is:

Code: Select all

import pygame
import time

def play_beep(frequency, duration):
    pygame.mixer.init()
    beep_sound = pygame.mixer.Sound("/usr/share/sounds/LinuxMint/stereo/desktop-login.ogg")  # You can use a different sound file if you prefer
    beep_sound.play()
    time.sleep(duration)
    pygame.mixer.quit()

if __name__ == "__main__":
    frequency = 1000  # Adjust the frequency as needed
    duration = 1.0  # Adjust the duration in seconds

    play_beep(frequency, duration)
promptly at 10:16am an error log spawned on my Desktop. It reads:


"ALSA lib pcm.c:8568:(snd_pcm_recover) underrun occurred
pygame 2.5.2 (SDL 2.28.2, Python 3.10.12)
Hello from the pygame community. https://www.pygame.org/contribute.html"

Does this give anyone any ideas of what is going wrong?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Running desktop scripts from cron

Post by Logansfury »

THE BOT GOT IT :D :D :D

I actually coaxed the proper crontab entry out of ChatGPT3.5 :D

The .wav worked, the wallpaper change worked, the pause worked, the wallpaper restore worked, THE WHOLE BASH SCRIPT WORKED!!!!

Here is the crontab entry:

40 18 * * * export DISPLAY=:1 && export
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus && export
XDG_CURRENT_DESKTOP=Cinnamon && export
XDG_RUNTIME_DIR=/run/user/$(id -u) && /usr/bin/paplay
$(/home/logansfury/xdotool/bash_random.sh) &&
/home/logansfury/xdotool/bash_random.sh

I am so freakin happy XP
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Scripts & Bash”