2

I'm trying to run the following script at startup. I am able to see the notification only when I execute the script from the terminal, or when I run it as a program. So the issue is basically when run by the Startup Manager.

Script:

#!/bin/bash

Error log file

ERROR_LOG_FILE="$HOME/onedrive_mount_error_log.txt" OUTPUT_LOG_FILE="$HOME/script_output_log.txt"

Redirect stdout and stderr to OUTPUT_LOG_FILE

exec > "$OUTPUT_LOG_FILE" 2>&1

Check if rclone is installed

if ! command -v rclone &> /dev/null; then echo "rclone not found. Exiting script." exit 1 fi

Mount OneDrive using rclone

rclone --vfs-cache-mode writes mount OneDrive: ~/OneDrive if [ $? == 0 ] ; then # If mount is successful, send a notification /usr/bin/notify-send "OneDrive Connected" "Microsoft OneDrive successfully mounted." else # If mount fails, log the error echo "Failed to mount OneDrive on $(date)" >> $ERROR_LOG_FILE /usr/bin/notify-send "OneDrive Connection Failed" "Rclone failed to mount OneDrive." fi

I'm using Ubuntu 23.10

Content of .confing/autostart/onedrive.sh.desktop

[Desktop Entry]
Type=Application
Exec=/home/ernesto/Documents/Scripts/test-message.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Delay=10
Name[en_US]=Autostart Test message
Name=Autostart Test message
Comment[en_US]=Autostart Test message
Comment=Sync Automatic
Raffa
  • 34,963
Anima94
  • 23

1 Answers1

2

rclone if run in the foreground (you might want to check the --daemon option) will block executing the consecutive line in your script for as long as it's maintaining that mount (which is actually the whole time it's mounted)

One way is by increasing the rclone command's verbosity and parsing its output (you need to observe the output to know what to look for, but I did that already):

#!/bin/bash

rclone -vv --vfs-cache-mode writes mount OneDrive: ~/OneDrive |& while read -r l do # If mounted: grep -q 'Mounting on' <<< "$l" && notify-send -i info -u critical "INFO: One Drive" "$l" # On error: grep -iq 'error' <<< "$l" && notify-send -i error -u critical "ERROR: One Drive" "$l" done

... make sure this part goes to the bottom of your script or else it will block executing subsequent lines.

Notice there is Bash (and alike shells) specific syntax in that ... If using a POSIX shell, then you might want to change |& to 2>&1 | and grep ... <<< "$l" to printf '%s\n' "$l" | grep ...

Another way is (although a bit more complicated):

#!/bin/bash

m="$(rclone --vfs-cache-mode writes mount OneDrive: ~/OneDrive --daemon 2>&1)"

if [ "$?" -eq 0 ] then while pgrep -f "rclone --vfs-cache-mode writes mount OneDrive:" &> /dev/null do findmnt "$HOME"/OneDrive &> /dev/null && notify-send -i info -u critical "INFO: One Drive" "OneDrive successfully mounted ...\n" && break done else notify-send -i error -u critical "ERROR: One Drive" "Failed to mount OneDrive ...\n\n${m}\n" fi

Raffa
  • 34,963