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
  • 35,113
Anima94
  • 23
  • 1
    It's not related to 23.10 for sure (I tested it) @user68186 ... Anima94, can you redirect both standard output and standard error streams of the script to a file and see what messages are printed if any ... also please check the system logs for the previous 10 minutes after your script runs with e.g. journalctl --since='-10 minutes' --no-pager – Raffa Jan 07 '24 at 09:05
  • Your system seems to take more than 5 minutes to boot, why is that? ... However, the related service org.gnome.Shell.Notifications seems to start successfully ... Try monitoring by running in one terminal window dbus-monitor | grep -10 notify-send and in another terminal window notify-send "My Title" "My Message" ... You'll see the message being sent ... Now delay your script with e.g. sleep 120 then re-login to start monitoring in the terminal and see if the message is sent ... dbus-monitor alone without | grep ... will give you more details. – Raffa Jan 07 '24 at 11:40
  • You run dbus-monitor in the terminal and it will monitor messages going through the D-Bus message bus i.e. the route a notify-send message takes until it appears in the notifications area ... It will monitor the messages sent by your script in real-time so you need to delay the execution of your startup script by enough seconds e.g. 120 so that you can open a terminal and start running dbus-monitor before it starts executing ... I would recommend you try it first manually in two terminal windows so that you know how it works and what you should expect later when your script runs. – Raffa Jan 07 '24 at 12:22
  • There you go :-) ... The message is never sent from your script or never reach the message bus ... Find out why ... Notice that [ $? == 0 ] in your conditional will always evaluate the exit code of the previous command i.e. echo "rclone exited with = $?" > "$HOME/exit_code.txt" and that is probably not what you expect (you probably expect the exit code of rclone ...) ... Start by making your shebang like this #!/bin/bash -x which will print command execution traces ... Redirect the output of your whole script to a file so that you can read it later. – Raffa Jan 07 '24 at 12:44
  • OK, let us see if the message is actually sent from the script ... Please (in your script) change the line /usr/bin/notify-send "Text Message" "My message" to strace /usr/bin/notify-send "Text Message" "My message" and add one line before that line with exec &> "$HOME"/my.log ... Re-login and wait for your startup script to execute and then run cat ~/my.log and copy the output to your question ... Notice a log file named my.log will be created under your home directory (you can delete it after you copy the output into your question) – Raffa Jan 07 '24 at 15:20
  • @user68186 You're most welcome ... I'm about to run out of ideas too :-) ... But, let's trace it and see if it actually sends the message to the current user's bus and if yes, then I might join you. – Raffa Jan 07 '24 at 15:23
  • I don't know if is needed, but now it is working. I think you guys were right all along, it is the if else code that is messing with the notify-send. So having only those three line it works. I see the notification. As ssoo as i put the if else... with all the rclone thing going on, it doesn't work. I tried before to only put a notify-send and it wasn't working. now it is.. I don't know how to explain it. So yes, the issue must be the if-else of the code i think. – Anima94 Jan 07 '24 at 16:47
  • Done. I've also updated the code in the main post. Now the notification is not working again, but the rclone command get executed correctly as i can see the drive mounted succesfully. Also, the file generated script_output_log.txt" is empty ( i thought you wanted to know this ) – Anima94 Jan 07 '24 at 17:05
  • OK, I will throw an educated guess ---> Your script could be exiting immaturely even before it reaches the conditional statement because there is an extra carriage return character (or other rich text/HTML character) at the end of the line rclone --vfs-cache-mode writes mount OneDrive: ~/OneDrive (have you copied it from somewhere or edited your script on Windows) ... Run cat -A /home/ernesto/Documents/Scripts/test-message.sh to know ... And in all cases try creating a new script file and type every line (don't copy) then use it instead. – Raffa Jan 07 '24 at 17:13
  • I did and nothing changed but I've discovered something else. So If I just type these three lines: `#!/bin/bash

    Mount OneDrive using rclone

    rclone --vfs-cache-mode writes mount OneDrive: ~/OneDrive /usr/bin/notify-send "OneDrive Connected" "Microsoft OneDrive successfully mounted."`

    The notification stops. If I comment the line of rclone, like so: #!/bin/bash

    Mount OneDrive using rclone

    rclone --vfs-cache-mode writes mount OneDrive: ~/OneDrive

    /usr/bin/notify-send "OneDrive Connected" "Microsoft OneDrive successfully mounted."

    – Anima94 Jan 07 '24 at 17:41
  • It works. The notification works. So something happens when the rclone runs, the notification jsut never get reached. – Anima94 Jan 07 '24 at 17:41
  • 1
    Another update, IF i add the character '&' after the line of Rclone. The notification works again. So rclone command is preventing the terminal or the script to go past itself. Is like it is blocking the script there? – Anima94 Jan 07 '24 at 17:49
  • ... or maybe rclone just takes a very long time to clone the files and that's what is delaying the notification ... as you know the rclone command must finish (unless you send it to the background as you did by appending &) before the subsequent lines are parsed and executed by the shell. – Raffa Jan 07 '24 at 17:58
  • Right. So we need to basically tell the script to keep waiting untill all is done? Thing is that I can see the drive mounted properly with all the file etc inside. Maybe could be a good idea to delegate the check to another script? I feel such a noob on this things.. – Anima94 Jan 07 '24 at 18:04
  • You actually might like Bash's "job control" ... See it discussed and demonstrated in action here (halfway that answer, it begins): https://askubuntu.com/a/1461645 ... Happy coding :-) – Raffa Jan 07 '24 at 18:13
  • That's sooo advanced for me ahaha I'm too old. But at least we tried. Thank you guys for your enormus patience and disponibility. – Anima94 Jan 07 '24 at 18:15
  • You're most welcome. – Raffa Jan 07 '24 at 18:16
  • I'm going to try your idea of putting everything on one line and let you know. Regards the & it doesn't work, all it does it doesn't stop the script and so it goes on. But it's not a solution a d I cannot mark any answer as solved. The issue is not been solved unfortunately. We tried everything possible me and Raffa. And you of course. – Anima94 Jan 09 '24 at 07:13

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
  • 35,113
  • Hey @Raffa I used your code but I'm getting some weird notifications. The drive is connected successfully, but the notification is weird. Here the picture of what I'm getting ( actually is a series of notification with "DEBUG" as prefix: https://ibb.co/yyxzG38 – Anima94 Jan 09 '24 at 17:25
  • @Anima94 Yep, it reports all errors including temporary connection/resolving/synchronizing ... But it did notify you about the successful mount, didn't it? ... I expected you to inspect the output and modify the text parsing method to your liking so it displays only what you want. – Raffa Jan 09 '24 at 17:41
  • Thanks, I will have a look at it. But it definitly is the best solution so far. Is all working. Thank you!!! – Anima94 Jan 09 '24 at 18:33
  • @Anima94 You are welcome enthusiastic young programmer :-) ... Give me a short while and I will update the answer with another totally different method that works and you might find more convenient than the one already described ... Although might not be as simply put. – Raffa Jan 09 '24 at 18:39
  • @Anima94 Done that ... Please test the second way and let me know if you need help with that. – Raffa Jan 09 '24 at 19:18
  • RAFFA YOU'RE A LEGEND! It works perfectly!!! Now I just need to study it properly to understand what you have done – Anima94 Jan 10 '24 at 19:22