If you wish to have your USB pen drive auto-mounted upon insertion using udev then here is the method to do that. Name a file by the name of your choice but must end as ".rules". Copy this file to location: /etc/udev/rules.d. After copying it restart the udev service and you should have auto-mounting of USB to work!
----------------------------------------------------------------------------------------------------------
#custom USB pen drive auto mounting rule via udev
ACTION=="add", KERNEL=="sd[a-z]?", NAME="%k", SYMLINK+="usb-%n", GROUP="users", OPTIONS+="last_rule"
ACTION=="add", KERNEL=="sd[a-z]?", RUN+="/bin/mkdir -p /mnt/usb", RUN+="/bin/mount -t vfat -o rw,flush /dev/%k /mnt/usb"
ACTION=="remove", KERNEL=="sd[a-z]?", RUN+="/bin/umount -l /mnt/usb"
ACTION=="remove", KERNEL=="sd[a-z]?", RUN+="/bin/rmdir /mnt/usb", OPTIONS+="last_rule"
-----------------------------------------------------------
Monday, December 16, 2013
Add and remove script from startup
Many a times we need to add scripts to startup. I have made a bash script which allows you to add any custom script of your choice to be added to startup and also to remove it. You may also add your custom run and stop priority to the script.
-------------------------------------------------------------------------------------------------------------
#! /bin/bash
####################################################################
#Author :Rohit Sivakumar
#Date :01NOV2013
#Version :1.0
#Description :This script will add your script to automatic #startup with defaults run-order. This script takes two #arguments(add and remove). It can also remove the script from startup.
#This script should reside in your home directory path:
# /home/<yourloginName>
####################################################################
linkName="$(echo $1 | cut -d'.' --complement -f2-)"
if [ "$2" = "add" ]; then
echo "${1} will be added to automatic startup now."
sudo ln -s ~/$1 /etc/init.d/$linkName
sleep 1
sudo chmod +x /etc/init.d/$linkName
cd /etc/init.d/
if [ -z "$3" ]; then
echo "Default start and stop runlevels selected."
sudo update-rc.d $linkName defaults
else
if [ "$3" -eq "$3" ]; then
if [ -z "$4" ]; then
echo "Custom start run level set but no stop runlevel customization."
sudo update-rc.d $linkName defaults $3
echo "Job done."
else
if [ "$4" -eq "$4" ];then
echo "Custom start and stop run level set."
sudo update-rc.d $linkName defaults $3 $4
echo "Job done."
fi
fi
else
echo "Run levels have to be numeric."
fi
fi
cd ~
elif [ "$2" = "remove" ]; then
echo "${1} will be removed from automatic startup now."
cd /etc/init.d/
sudo update-rc.d -f $linkName remove
sleep 1
sudo rm /etc/init.d/$linkName
cd ~
echo "Cleanup completed."
else
echo "You have not provided proper argument to add or to remove."
fi
--------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
#! /bin/bash
####################################################################
#Author :Rohit Sivakumar
#Date :01NOV2013
#Version :1.0
#Description :This script will add your script to automatic #startup with defaults run-order. This script takes two #arguments(add and remove). It can also remove the script from startup.
#This script should reside in your home directory path:
# /home/<yourloginName>
####################################################################
linkName="$(echo $1 | cut -d'.' --complement -f2-)"
if [ "$2" = "add" ]; then
echo "${1} will be added to automatic startup now."
sudo ln -s ~/$1 /etc/init.d/$linkName
sleep 1
sudo chmod +x /etc/init.d/$linkName
cd /etc/init.d/
if [ -z "$3" ]; then
echo "Default start and stop runlevels selected."
sudo update-rc.d $linkName defaults
else
if [ "$3" -eq "$3" ]; then
if [ -z "$4" ]; then
echo "Custom start run level set but no stop runlevel customization."
sudo update-rc.d $linkName defaults $3
echo "Job done."
else
if [ "$4" -eq "$4" ];then
echo "Custom start and stop run level set."
sudo update-rc.d $linkName defaults $3 $4
echo "Job done."
fi
fi
else
echo "Run levels have to be numeric."
fi
fi
cd ~
elif [ "$2" = "remove" ]; then
echo "${1} will be removed from automatic startup now."
cd /etc/init.d/
sudo update-rc.d -f $linkName remove
sleep 1
sudo rm /etc/init.d/$linkName
cd ~
echo "Cleanup completed."
else
echo "You have not provided proper argument to add or to remove."
fi
--------------------------------------------------------
Get the PID of a process
Many a times, one needs to get the PID of a process to do some additional tasks on it.
Here is a script which could be added into other scripts where you need such a requirement so as to kill a certain process if running, start a process if not found and many such cases!
--------------------------------------------------------------------------------------------------------------
#!/bin/bash
#This script is to be used as ./testPID <processname>
PID=$(pgrep $@)
#if [ -z "$PID" ] is same as writing if[ "$PID" = "" ]
if [ "$PID" = "" ]; then
echo "Process not found"
else
echo "Process[PID]: $@[$PID]"
fi
----------------------------------------------------------------------------------------------------------------
Here is a script which could be added into other scripts where you need such a requirement so as to kill a certain process if running, start a process if not found and many such cases!
--------------------------------------------------------------------------------------------------------------
#!/bin/bash
#This script is to be used as ./testPID <processname>
PID=$(pgrep $@)
#if [ -z "$PID" ] is same as writing if[ "$PID" = "" ]
if [ "$PID" = "" ]; then
echo "Process not found"
else
echo "Process[PID]: $@[$PID]"
fi
----------------------------------------------------------------------------------------------------------------
Know your external IP
This bash script helps to identify the external IP address using ipecho.net service. There are several ways to finding an external IP of a system but this is one where you can quickly get it in your script and use it for in your programming!!
--------------------------------------------------------------------------------------------------------------
#!/bin/bash
# This script gets you the external IP address of your system.
EXT_IP=$(curl ipecho.net/plain 2>/dev/null)
LOCAL_IP=$(hostname -I 2>/dev/null)
HOSTNAME=$(hostname -f 2>/dev/null)
printf "\n***********************************************\n"
printf "Your External IP address is:"$EXT_IP"\n"
printf "Your Machine's Host name is:"$HOSTNAME"\n"
printf "Your Local IP is:"$LOCAL_IP"\n"
printf "\n***********************************************\n"
printf "Have a great day!\n\n"
----------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
#!/bin/bash
# This script gets you the external IP address of your system.
EXT_IP=$(curl ipecho.net/plain 2>/dev/null)
LOCAL_IP=$(hostname -I 2>/dev/null)
HOSTNAME=$(hostname -f 2>/dev/null)
printf "\n***********************************************\n"
printf "Your External IP address is:"$EXT_IP"\n"
printf "Your Machine's Host name is:"$HOSTNAME"\n"
printf "Your Local IP is:"$LOCAL_IP"\n"
printf "\n***********************************************\n"
printf "Have a great day!\n\n"
----------------------------------------------------------------------------------------------------------------
Subscribe to:
Posts (Atom)