Fdisk and grep command not working in udev trigger


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Fdisk and grep command not working in udev trigger

Hi Guys,

Can someone take a look at my scripts what missing, plugin usb drive the script is running can log all my echo but cannot execute command. Is there any configuration in linux or to my scripts need to add?.

What i want to achieve is every time I plugin the usbdisk automatic mount to /media/usb/test

Code:
cat /etc/udev/rules.d/100-mount-usb.rules

ACTION=="add", SUBSYSTEMS=="usb", ATTR{idVendor}=="8564", ATTR{idProduct}=="1000", RUN+="/bin/bash -c /usr/local/bin/test.sh"

Code:
cat /usr/local/bin/test.sh

#!/bin/bash

LOG_FILE=/opt/weekly_backup.log

DEVNAME=$(fdisk -l |grep FAT16 |awk '{print $1}')

echo "New usb device detected at $DEVNAME" >> $LOG_FILE

echo "mount $DEVNAME /media/usb/test" >> $LOG_FILE

mount $DEVNAME /media/usb/test &>> $LOG_FILE

Reload all rules in udev:
Code:
udevadm control --reload-rules

Trigger all udev rules:
Code:
udevadm trigger

Here the log ouput:
Code:
cat /opt/weekly_backup.log

New usb device detected at
mount  /media/usb/test
mount: can't find /media/usb/test in /etc/fstab or /etc/mtab


Last edited by lxdorney; 09-12-2018 at 01:06 PM..
# 2  
Which distribution of Linux??
# 3  
Just a shot in the dark: did you consider supplying the full pathnames?
# 4  
Quote:
Originally Posted by RudiC
Just a shot in the dark: did you consider supplying the full pathnames?
Yes sir I did tried /bin/fdisk, /bin/grep

------ Post updated at 05:47 PM ------

Quote:
Originally Posted by hicksd8
Which distribution of Linux??
CentOS 6.8 Sir
# 5  
Looking at the output(mount /media/usb/test), DEVNAME is not set. It looks like the previous fdisk command is not returning anything. What is fdisk -l |grep FAT16 |awk '{print $1}' returning if you run it manually?

If DEVNAME isn't set, the mount command won't know which device to mount, and will look in the fstab / mtab for /media/usb/test to figure out what to do.
# 6  
Quote:
Originally Posted by Scott
Looking at the output(mount /media/usb/test), DEVNAME is not set. It looks like the previous fdisk command is not returning anything. What is fdisk -l |grep FAT16 |awk '{print $1}' returning if you run it manually?

If DEVNAME isn't set, the won't get the name, and will look for it in the fstab / mtab for how / what to mount.
Supposedly it will return /dev/sdb1 in my case, already tried manually like this DEVNAME="/dev/sdb1" and put echo $DEVNAME and i see the output, but is not mounting
# 7  
"Supposedly" doesn't really instil confidence Smilie You tried setting DEVNAME manually, and that also didn't work?

Is all of the code in the same script.. e.g. did you export DEVNAME if it's not?

If you set DEVNAME manually, after (or in place of) the fdisk command, there's no reason that the output would be
Code:
mount /media/usb/test

instead of
Code:
mount /dev/sdb1 /media/usb/test

If the variable DEVNAME is not created as read-only (e.g. gives an error why you try to set it), and the script is not being run in a shell jail (chroot environment), that is.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #851
Difficulty: Medium
Angular is a TypeScript-based open-source web application framework.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command is not working

I have made a program that reads a text file and checks for palindromic words and then outputs them. They each appear on a new line with a count of the number of occurences beside each of the words. Requirements for being classed as palindrome are that the word must have at least 3 letters and... (7 Replies)
Discussion started by: greenhouse91
7 Replies

2. BSD

OpenBSD fdisk - Linux fdisk compatibility ?

Hello, MBR partition table made by linux fdisk looks certainly not correct when printed by openbsd fdisk: Partition table created on linux (centos 6.3): # fdisk -l /dev/sdc Disk /dev/sdc: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 *... (2 Replies)
Discussion started by: vilius
2 Replies

3. Shell Programming and Scripting

problem in automating "fdisk" command using send and expect

hi i want to automate fdisk command . i spawned a process containing fdisk command from a process and tried to send the options to fdisk promt from that process. but that spawed process is notstarting itself help me out trying for two days :wall: my code: #!/bin/bash echo... (5 Replies)
Discussion started by: jagak89
5 Replies

4. Solaris

what is the fdisk -l command of SUN

I use fdisk -l command to see the attached hard disk drives in rhel5 and cntos 5.5 what is the same command for sun 5.9 (4 Replies)
Discussion started by: z_haseeb
4 Replies

5. Shell Programming and Scripting

Pass params with Udev

Hello! I'm sorry if this is the false Forum, didn't really knew where to put it... My question: I have serveral USB-Sticks and wrote several Udev-Rules for theme, each Sticks needs to do something else, but all are using the same script (they have common tasks to do) and only some parts are... (2 Replies)
Discussion started by: al0x
2 Replies

6. Filesystems, Disks and Memory

Udev label removes corresponding fdisk, sfdisk or lsvdev entry

I'm curious about the behavior where any udev labeled device causes that corresponding listing to disappear from fdisk, sfdisk, or in the case of RDAC, lsvdev. I have seen this on both EMC clariion and Sun Storagetek/Engenio 6540 arrays. We use RHEL5.1 and udev to create persistent labels for... (2 Replies)
Discussion started by: Radar
2 Replies

7. Shell Programming and Scripting

Grep command is not working when put into cron

Hi, I worte a script which runs perfect when i execute it manually. But when i scheduled into cron the grep command alone is not working. the sample script, /usr/bin/grep FTP $subfile > /tmp/tfsrec.dat tfs=`echo $?` if then echo "FTP FOUND" else echo "FTP NOT FOUND" Where... (5 Replies)
Discussion started by: thiru_cs
5 Replies

8. Shell Programming and Scripting

Fdisk with grep problem

Hello! rescuecd:/var# fdisk -l | grep stupid Disk /dev/sda doesn't contain a valid partition table Disk /dev/sdb doesn't contain a valid partition table rescuecd:/var# It shows always this statement. Why? :( Raw fdisk -l shows rescuecd:/var# fdisk -l Disk /dev/sda: 750.1 GB,... (4 Replies)
Discussion started by: pug123
4 Replies

9. Shell Programming and Scripting

How can I trigger another make command when one is finished?

Hello all I have to run manually make commands in our system the make compilations task's takes very long And I like to be able to run another make task right after one is finished. What is the best way to automate it ? (2 Replies)
Discussion started by: umen
2 Replies

10. Linux

udev core 5

installed fedora core 5 on a pc with USB and some usual things. in boot up it is stopped at "Starting udev:". Its harddisk light is busy. is it reconfiguring the kernel?. what do to solve this problem?. (0 Replies)
Discussion started by: GJ2
0 Replies

Featured Tech Videos