4

I'd like to use notify-send from within a bash script that is running in the background to inform the user about the progress of the script. More specifically this is a script that automagically runs when a USB flash drive is inserted and runs a scan with ClamAV.

Specifically at line 30 and line 66. So far, I'm not having any luck. Can someone give me some advice/help? Thanks.

#!/bin/bash
#doOnUSBinsert_0.2.sh
#Author : Totti
# Make it executable by running 'sudo chmod  x doOnUSBinsert_0.2.sh'


if ! [ -f /etc/udev/rules.d/80-doOnUSBinsert.rules ]
then        # rule not added
   cp "$0" /usr/bin/doOnUSBinsert
   chmod u x /usr/bin/doOnUSBinsert

#   echo 'SUBSYSTEM=="usb", ACTION=="add", RUN ="/path/to/script.sh"' | sudo tee     /etc/udev/rules.d/80-clamscan.rules
   echo 'SUBSYSTEM=="usb", ACTION=="add", RUN ="/usr/bin/doOnUSBinsert & "' | tee     /etc/udev/rules.d/80-doOnUSBinsert.rules
   if  [ $? -eq 0 ]
   then
     echo 'Rule Successfully added. See file "/usr/bin/doOnUSBinsert" if you wish to edit the command'
     exit 0
    else
     echo 'ERROR while adding rule'
     exit 1
   fi
fi



lfile="/tmp/doOnUSBinsert.log"     # udev
lfile2="/tmp/clamscanFromUdev.log"   # clamscan
lfile3="/tmp/doOnUSBinsert_mount.log"   # mount

notify-send "USB SCAN ON INSERT" "Currently scanning with ClamAV"

main ()
{
sleep 12  # let the partitions to mount

   #cat /proc/$$/environ | tr '�' 'n' >> /tmp/udevEnvirn.txt
echo "found $ID_SERIAL"   >> "$lfile"
  cat /etc/mtab | grep "^$part_c"   >> "$lfile.3"

if [ "$ID_SERIAL"x = 'x' ]
then
 echo "Exiting on empty ID_SERIAL"   >> "$lfile"
 exit 1
fi

#Eg: ID_SERIAL --> /dev/disk/by-id/usb-sandisk....42343254343543
#i=0
echo 'searching partitions'   >> "$lfile"

for partitionPath in  $( find /dev/disk/by-id/ -name "*$ID_SERIAL*part*" )
do
  echo "current partition = $partitionPath"   >> "$lfile"
 # part[i  ]="$( readlink -f "$partition" )"        # Eg Output: /dev/sdb1     , /dev/sdb2
  part_c="$( readlink -f $partitionPath )"   
  mpoint="$( cat /etc/mtab | grep "^$part_c"  | awk '{print $2}' )"

  echo "partitionPath= $partitionPath, part = $part_c, mountpoint=  $mpoint"  >>     "$lfile"

  echo "Scaning -->  $mpoint"  >> "$lfile.2"
  ############################################
  clamscan -r --bell "$mpoint"/*  >> "$lfile.2"
  #############################################
done
}

notify-send "USB SCAN ON INSERT" "Finished scanning with ClamAV"

main &
echo ______________________________________  >> "$lfile"
exit 0

3 Answers 3

2

I'm pretty new to the linux world, but while looking for a solution for a similar project I found THIS

Tip: An overview on the available icons can be found here. To send desktop notification from a background script running as root (replace X_user and X_userid with the user and userid running X respectively):

sudo -u X_user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/X_userid/bus notify-send 'Hello world!' 'This is an example notification.'

Hope this will help others.

Sign up to request clarification or add additional context in comments.

Comments

1

Depending on how you are running the script it may not have access to the display variable. Try running export DISPLAY=:0.0 prior to the command.

If you are running the script as a different user, ie root, then you may also need to run it as su - <logged in user> -c notify-send ... (I usually don't need to do this, but I remember having to at one point - but I cant recall which distro or version I was on at the time.)

1 Comment

Ok, thanks - it worked on Ubuntu 16.04.3 LTS with Unity: export DISPLAY=:0.0 su - $USER -c "/usr/bin/notify-send 'USB SCAN' 'Currently scanning USB with ClamAV'"
0

After many trials and errors, here's what works for me to notify all connected users, with notify-send, within a bash script executed as root with cron (weekly), on Linux (Fedora 38/GNOME 44):

for n in `who | awk '!_[$1]++ {print $1}'`; do sudo -u "$n" DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(sudo -u "$n" id -u)/bus notify-send "Message to users"; done

You may note that it can be run with just one command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.