How Can I Run My Script in the Background on GNOME Login?

0
10
Asked By CuriousCat72 On

I'm trying to set up a script to run automatically when I log into my GNOME session. I've created a desktop entry file in ~/.config/autostart named inotifyOCRFolderScans.desktop with the following content:

```
[Desktop Entry]
Version=1.0
Name=AutomaticOCR
Comment=Check for newly scanned files, run OCR, and clean up originals
Exec=/home/jens/bin/automaticOCR.sh
Terminal=true
Type=Application
Hidden=false
```

It works fine and launches the terminal when 'Hidden=false'. However, if I set 'Hidden=true' or 'Terminal=false', the script doesn't run at all. I want the script to execute without needing a terminal window. Here's my script, which is executable:

```
#! /bin/bash
scandir=~/Dokumente/scans
inotifywait -m -e moved_to,create $scandir --quiet | while read directory event name; do
if [[ $name == *.tmp ]]; then
echo "skip temporary files"
elif [[ $name == *.pdf ]]; then
ocrmypdf -c -l deu --output-type pdf "$directory$name" "$scandir/ocr/$name"
rm "$directory$name"
fi
done
```

I'm using Manjaro with GNOME 49.2, and my logs don't show any errors related to autostart or the script. How can I modify this to launch in the background properly?

3 Answers

Answered By ScriptSavvy On

You might also want to consider this approach: Instead of using `Hidden=true`, just set it to `false` if you want it to run quietly. If you want it completely in the background without a terminal, you can write to a log file instead of using `echo`. Also, make sure that your script's directories and files have the appropriate permissions to avoid access issues.

Here's a quick example:
```
mkdir -p /home/jens/Dokumente/scans/ocr
```
This way, your ocr directory is created if it doesn't exist before you try to use it. This also aligns with error prevention tips!

Answered By TechieTinker On

It sounds like you're on the right track! When you set `Hidden=true`, GNOME doesn't launch your script at all; it's more like a way to disable it without deleting. As for `Terminal=false`, that means it won't allocate a terminal, so if your script relies on any terminal-related environment, it won't work.

Here's what you can try:

In your .desktop file, use:
```
[Desktop Entry]
Type=Application
Name=AutomaticOCR
Comment=Automatic OCR scan processing
Exec=/bin/bash -lc '/home/jens/bin/automaticOCR.sh'
Terminal=false
X-GNOME-Autostart-enabled=true
```

Using `bash -lc` ensures that your script runs in a login shell where all environment variables are set correctly.

Also, make sure not to use `~` in your script path. Change `scandir=~/Dokumente/scans` to `scandir="/home/jens/Dokumente/scans"`. This prevents potential issues when the script runs.

Finally, consider adding error handling to your script to avoid silent failures! Just a little bit of code to manage error states can help. Good luck!

Answered By HappyHelper On

Thank you soooo much! That works as intended!!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.