# Fdisk and grep command not working in udev trigger

**URL:** <https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688>\
**Category:** Shell Programming and Scripting\
**Created:** [September 12, 2018, 10:24am UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688 "2018-09-12T10:24:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 10:24am UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/1 "2018-09-12T10:24:27Z")

</div>

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

```nohighlight
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"

```

```nohighlight
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:

```nohighlight
udevadm control --reload-rules

```

Trigger all udev rules:

```nohighlight
udevadm trigger

```

Here the log ouput:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![hicksd8](https://community.unix.com/letter_avatar/hicksd8/32/6_449c533f586939b6e5f246632a46421f.png) [@hicksd8](https://community.unix.com/u/hicksd8)\
**Post date:** [September 12, 2018, 12:09pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/2 "2018-09-12T12:09:24Z")

</div>

Which distribution of Linux??

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [September 12, 2018, 2:54pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/3 "2018-09-12T14:54:40Z")

</div>

Just a shot in the dark: did you consider supplying the full pathnames?

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 6:47pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/4 "2018-09-12T18:47:15Z")

</div>

> [@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 ------

> [@hicksd8](#):
>
> Which distribution of Linux??

CentOS 6.8 Sir

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 12, 2018, 6:50pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/5 "2018-09-12T18:50:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 6:58pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/6 "2018-09-12T18:58:06Z")

</div>

> [@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

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 12, 2018, 6:59pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/7 "2018-09-12T18:59:58Z")

</div>

"Supposedly" doesn't really instil confidence 🙂 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

```nohighlight
mount /media/usb/test

```

instead of

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 7:11pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/8 "2018-09-12T19:11:24Z")

</div>

> [@scott](#):
>
> "Supposedly" doesn't really instil confidence 🙂 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
> 
> ```plaintext
> mount /media/usb/test
> 
> ```
> 
> instead of
> 
> ```plaintext
> mount /dev/sdb1 /media/usb/test
> 
> ```
> 
> If the variable DEVNAME is not created as read-only, or you're not running the script in a shell jail, that is.

Yes Sir you right the output

```plaintext
mount /dev/sdb1 /media/usb/test

```

but mounting is not working

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 12, 2018, 7:13pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/9 "2018-09-12T19:13:40Z")

</div>

Do you have permissions to mount filesystems?

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 7:16pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/10 "2018-09-12T19:16:51Z")

</div>

> [@scott](#):
>
> Do you have permissions to mount filesystems?

I dont know where can i add permission, the trigger of my script when the usbdisk plugi it in. Do you have any idea?

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 12, 2018, 7:31pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/11 "2018-09-12T19:31:24Z")

</div>

Generally speaking, to mount a filesystem, you need root privileges. If you're running your script as a non-root user, you can get root privileges for the purpose of mounting the FS, using sudo. If you administer the system you can easily do this by adding something to the "sudoers" file (with the visudo command, not vi), for example, from the sudoers file in CentOS 6:

```nohighlight
## Allows members of the users group to mount and unmount the
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

```

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 8:02pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/12 "2018-09-12T20:02:25Z")

</div>

> [@scott](#):
>
> Generally speaking, to mount a filesystem, you need root privileges. If you're running your script as a non-root user, you can get root privileges for the purpose of mounting the FS, using sudo. If you administer the system you can easily do this by adding something to the "sudoers" file (with the visudo command, not vi), for example, from the sudoers file in CentOS 6:
> 
> ```plaintext
> ## Allows members of the users group to mount and unmount the
> ## cdrom as root
> # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
> 
> ```

```plaintext
%users

```

is replace my user as

```plaintext
root

```

sir? In my visudo put this

```plaintext
root ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom, /bin/fdisk /bin/grep

```

is this right sir?

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 12, 2018, 8:20pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/13 "2018-09-12T20:20:17Z")

</div>

OK, it's late and I need to go. Let's speed this up.

Are you root? If you are, what error do you get when you try to mount the filesystem manually? A media error, an unsupported type error, an unknown / unsupported FS type error, or what?

Please try to be more forthcoming and communicative with information so we don't end up with a War and Peace-sized thread for what is probably a simple problem.

If you're not root, then the sudo rule you need to add should be for the user you want to allow to be superuser for the purposes of running the command - which won't be root, as root doesn't need sudo to run anything - the su in sudo means super user.. root is already a superuser.

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 8:55pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/14 "2018-09-12T20:55:29Z")

</div>

If manually mount the

```nohighlight
/dev/sdb1 /media/usb/test

```

no error sir, my script works run manually in terminal,but is not working from udev trigger when plugin the usbdisk.

Thanks for your time

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 12, 2018, 9:11pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/15 "2018-09-12T21:11:58Z")

</div>

Ah, bajeezus.. I somehow forgot this was about udev! Are there any errors in the system logs (dmesg, etc.)?

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 12, 2018, 9:20pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/16 "2018-09-12T21:20:12Z")

</div>

Here what happened when issued run

```nohighlight
udevadm trigger

```

manually.

```nohighlight
cat 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

```

and here the logs also when plugin my usb to the server, which is I get sameoutput in my log.

```nohighlight
cat 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

```

Im running in Centos 6.9

Thanks

------ Post updated at 08:20 PM ------

no error in dmesg sir

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [September 12, 2018, 9:26pm UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/17 "2018-09-12T21:26:52Z")

</div>

OK, well apologies for the slight tangent I sent us off on, but again it comes back to this "DEVNAME" thing.

Before, or after, when you call fdisk..

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

```

can you add some debug to the log file?

```nohighlight
fdisk -l |grep FAT16 |awk '{print $1}' 2>&1 >> $LOG_FILE

```

It seems that's not doing what you think it should be. Again, as previously suggested, you should fully qualify the commands (/usr/bin/grep, etc.) - shouldn't make any assumptions about the environment udev is using when it does stuff.

If you change all of the "\>\> $LOG\_FILE" to "&\>\> $LOG\_FILE", you'll also log all the errors, if there are any.

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 13, 2018, 10:21am UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/18 "2018-09-13T10:21:16Z")

</div>

```nohighlight
cat /usr/local/bin/test.sh

#!/bin/bash

LOG_FILE=/opt/weekly_backup.log

DEVNAME=$(/sbin/fdisk -l |/bin/grep FAT16 |awk '{print $1}') 2>&1 >> $LOG_FILE

#fdisk -l |grep FAT16 |awk '{print $1}' 2>&1 >> $LOG_FILE

sleep 2

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

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

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

```

running this command

```nohighlight
udevadm trigger

```

is working here the output of log

```nohighlight
cat /opt/weekly_backup.log

New usb device detected at /dev/sdb1
mount /dev/sdb1 /media/usb/test

```

pluging the usb here the output:

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

```

but if I put manually the

```nohighlight
DEVNAME=/dev/sdb1

```

and plugin the usb is working

---

<div class="post-metadata">

**Author:** ![Azrael](https://community.unix.com/user_avatar/community.unix.com/azrael/32/2411_2.png) [@Azrael](https://community.unix.com/u/Azrael)\
**Post date:** [September 13, 2018, 10:43am UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/19 "2018-09-13T10:43:18Z")

</div>

I'll agree with Scott. It looks like something is not being set, or is not mounted to set. Maybe if you post the output of "mount" and "fdisk -l" we can see what's missing.

---

<div class="post-metadata">

**Author:** ![lxdorney](https://community.unix.com/letter_avatar/lxdorney/32/6_449c533f586939b6e5f246632a46421f.png) [@lxdorney](https://community.unix.com/u/lxdorney)\
**Post date:** [September 13, 2018, 10:49am UTC](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688/20 "2018-09-13T10:49:41Z")

</div>

Here's the output

```nohighlight
fdisk -l

Disk /dev/sdb: 8019 MB, 8019509248 bytes
255 heads, 63 sectors/track, 974 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x78a9e380

   Device Boot Start End Blocks Id System
/dev/sdb1 * 1 975 7830528 c FAT16
Partition 1 has different physical/logical endings:
     phys=(973, 254, 63) logical=(974, 250, 44)

```

[Next page](https://community.unix.com/t/fdisk-and-grep-command-not-working-in-udev-trigger/373688.md?page=2)
