App cant find character device file when running inside bash script - linux

im trying to start my serial port application inside a bash script. for some strange reason the application cannot fetch the /dev/ttyACM0 device file. if i type ls -la in /dev/ directly from a terminal the device file visible. When i try the same thing in a bash script every device file is visible except for the ttyACM0.
in terminal:
root#pc:~$ ls -la /dev/ttyAC*
crw-rw---T 1 root dialout 166, 0 Feb 18 17:25 /dev/ttyACM0
in script:
ls: cannot access /dev/ttyAC*: No such file or directory
All other device files are still visible.. Does someone have any idea how this is possible?
Here is the script:
#!/bin/bash
# gpio defs
mcu_reset=49 #gpio2_17
mcu_erase=50 #gpio2_18
dir_export="/sys/class/gpio/export"
dir_reset="/sys/class/gpio/gpio$mcu_reset"
dir_erase="/sys/class/gpio/gpio$mcu_erase"
# Initialise Reset and erase gpio
if [ ! -d "$dir_reset" ]; then
echo $mcu_reset > $dir_export
echo out > "$dir_reset/direction"
echo $mcu_erase > $dir_export
echo out > "$dir_erase/direction"
echo 0 > "$dir_erase/value"
echo 1 > "$dir_reset/value"
echo "* GPIO directories initialised!"
fi
# erase mcu flash
echo "* Erasing device..."
echo 1 > "$dir_erase/value"
sleep 0.5
echo 0 > "$dir_erase/value"
sleep 0.5
# reset device
echo "* Resetting device..."
echo 0 > "$dir_reset/value"
sleep 0.5
echo 1 > "$dir_reset/value"
sleep 0.5
# Flash device
#$(bossac -e -d -w -v -b argv[0])
ls -la /dev/ttyAC*
# reset device
echo "* Resetting device..."
echo 0 > "$dir_reset/value"
sleep 0.5
echo 1 > "$dir_reset/value"
sleep 0.5

Found the problem! When 'echo 1 > "$dir_reset/value" gets executed the mcu connected to linux resets. the sleep of 0.5 seconds was to short for the mcu to come up again.

Related

bash script exits with zero status even after kill signal

I keep getting zero status even after interrupting the script.
The first script
#!/bin/bash
## call the backup script
/usr/local/bin/backup 2>&1 >/dev/null
echo $?
backup
#!/bin/bash
exitscript() {
rm -f $LOCKFILE
echo "Script Status: $1 | tee -a ${LOG}"
echo "> End Date: $(date +'%d.%m.%Y %H:%M:%S')" | tee -a ${LOG}
exit $1
}
######START#######
trap "exitscript 1" 1 2 23 24 25
rsync ${args} ${src} ${dest} | tee -a ${RSYNC_LOG}
retcode=${PIPESTATUS[0]}
if [[ ${retcode} -ne 0 ]]; then
exitcode=1
fi
exitscript ${exitcode:-0}
When the First Script is run, it returns exit status of 0 although i have tried to kill the backup script before it ends (for that i have created a very large size file so that rsync takes time to copy the file and i get the time to kill the script before it ends)
ps -ef | grep -i backup
kill $PID
Another thing is that even after killing the backup script, rsync still runs. I would like for rsync to stop once the script is being killed and my first script to return the status code of zero.
Much appreciation for any suggestions. Thanks!
I assume the missing quote in echo "Script Status: $1 | tee -a ${LOG} is not relevant to the question.
When you want a function to handle the trap, you need to export that function.
And when you want to kill children, you should add these in your trap-function.
I tested these adjustments with a sleep command, it should work for rsync too.
#!/bin/bash
exitscript() {
echo "Script Status: $1"
(( $pleasekill > 0 )) && kill ${pleasekill}
echo "> End Date: $(date +'%d.%m.%Y %H:%M:%S')"
exit $1
}
# Export the function exitscript
export exitscript
######START#######
pleasekill=0
trap "exitscript 1" 1 2 23 24 25
# Start I/O-friendly rsync function
sleep 30 &
pleasekill=$!
wait
exitscript 2
When you test this with the first script, use ^C or kill -1 pid_of_backup.

I see an incorrect pid

I have a problem in my script, when I want to save the pid, then the incorrect pid is saved to me.
I suspect that the pid script (start.sh) is written to me instead of the screen command.
echo "Trwa uruchamianie bota muzycznego..."
if [ -e "$BINARYNAME" ]; then
if [ ! -x "$BINARYNAME" ]; then
echo "${BINARYNAME} is not executable, trying to set it"
chmod u+x "${BINARYNAME}"
fi
if [ -x "$BINARYNAME" ]; then
export LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}"
screen -dmS "${BASENAME}" mono "${BINARYNAME}" > /dev/null &
TEST=$0
PID=$!
echo "${PID}"
ps -p ${PID} > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "Bot muzyczy nie został uruchomiony."
else
echo $PID > TS3AudioBot.pid
echo "Bot muzyczny został uruchomiony."
fi
else
echo "${BINARNAME} nie jest możliwy do wykrycia, nie można uruchomić bota muzycznego."
fi
else
echo "Could not find binary, aborting"
exit 5
fi
I believe you were expecting to get the pid of a screen process in $PID. What's happening is that screen exits immediately, and $! refers to the pid of the vanished screen process rather than the detached process that is running your mono command (if it's still running).
I substituted "sleep 2000 &" for "screen -dmS ${BASENAME}" mono ${BINARYNAME} >/dev/null &" in your script and the correct $PID, that of the sleep process, was saved in the variable and acted upon. This doesn't happen with screen for the reason I described above.
You might want to consider processing the output of "screen -list" in order to get at the pid of the detached process:
root#tutorial:/var/tmp# screen -dmS 'sleeper' sleep 2000
root#tutorial:/var/tmp# screen -list
There is a screen on:
7089.sleeper (07/02/2018 04:05:57 AM) (Detached)
1 Socket in /var/run/screen/S-root.
root#tutorial:/var/tmp# ps axlww | grep 7089
5 0 7089 1 20 0 25672 2396 poll_s Ss ? 0:00 SCREEN -dmS sleeper sleep 2000
4 0 7090 7089 20 0 5808 648 hrtime Ss+ pts/0 0:00 sleep 2000
0 0 7093 2607 20 0 12728 2192 pipe_w S+ ttyS1 0:00 grep 7089
From here your script could grab the pid of the sleeper.

xinitrc.d content are not applied when running startx

I have a script which allows me to restart my running Xserver. However, whenever the Xserver run back again, all the contents of xinitrc.d folder aren't applied.
rm /tmp/startx.logs
LOOPTC=0
while [ $LOOPTC -eq 0 ]
do
pidof TerminalConfig 1>/dev/null 2>/dev/null
LOOPTC=$?
sleep 1
echo Tc not closed >> /tmp/startx.logs
done
killall gdm 2>/dev/null &
pkill x
if grep ^AUTOLOGIN /etc/sysconfig/autologin | egrep "NO|no|No|nO" ; then
echo autologin off >> /tmp/startx.logs
LOOPX=0
while [ $LOOPX -eq 0 ]
do
pidof X 1>/dev/null 2>/dev/null
LOOPX=$?
sleep 1
echo X not closed >> /tmp/startx.logs
done
fi
clear >> /dev/tty1
for (( i=0; i<4; i++ )) ; do
sleep 1
echo Please wait while restarting X11 Windows... >> /dev/tty1
done
clear >> /dev/tty1
ps ax | grep startx > /tmp/startx.result
sed -e s/.*grep.*//g -e /^$/d /tmp/startx.result -i
echo $(date) :startx: >> /tmp/startx.logs
cat /tmp/startx.result >> /tmp/startx.logs
if [ -s /tmp/startx.result ] ; then
echo Thu Feb 1 22:50:08 UTC 2018 :startx already running, no need to execute startx >> /tmp/startx.logs
else
killall gdm 2>/dev/null &
startx
echo $(date) :startx not running, executing startx >> /tmp/startx.logs
fi
if grep --quiet if [ ! /etc/X11/xinit/xinitrc; then
cp -f /etc/X11/xinit/xinitrc.old /etc/X11/xinit/xinitrc
rm -f /tmp/startx.result
#rm -f /tmp/restart-x.sh
Note that this script is always run on the Xserver. All the xinitrc.d contents are always applied whenever I run startx from the console (without Xserver running).
I'm wondering, why aren't the xinitrc.d files applied even if I have already ensured that the contents of the xinitrc.d folder should be applied using a for-loop and placed it in the xinitrc file?

Recursion with for loops BASH

I have yet another question. This code is for the rcS script in a Linux distro I'm making and for some reason occasionally it segfaults. About 85% of the time it works just fine and all is dandy. But then there is that 15% of the time when it segfaults on either creating a directory or doing the mount.
# Find and mount all detected drives sda-sdm and partitions 0-10, you can change these by changing the values but odds are you won't have more than 26 USB drives plugged in or 10 partitions ;)
echo " ${YELLOW}[ ${BLUE}*${YELLOW} ] Finding and mounting the program drive"
for DRIVE in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
mkdir "/mnt/sd${DRIVE}/"
mount "/dev/sd${DRIVE}" "/mnt/sd${DRIVE}/" 2> /dev/null
if [ ! "${?}" = '0' ]; then
rmdir "/mnt/sd${DRIVE}/"
fi
for PARTITION in 1 2 3 4 5 6 7 8 9 10; do
mkdir "/mnt/sd${DRIVE}${PARTITION}"
mount "/dev/sd${DRIVE}${PARTITION}" "/mnt/sd${DRIVE}${PARTITION}/" 2> /dev/null
if [ ! "${?}" = '0' ]; then
rmdir "/mnt/sd${DRIVE}${PARTITION}/"
fi
if [ -d /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH} ]; then
if [ ! "${PROGRAM_DRIVE_FOUND}" = 'YES' ]; then
PROGRAM_DRIVE_FOUND='YES'
echo "${MESG} Found program drive on device sd${DRIVE}${PARTITION} mounted at /mnt/sd${DRIVE}${PARTITION}"
source /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH}/os_info
rm "/etc/init.d/os_info"
cp /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH}/os_info "/etc/init.d/os_info"
echo "${MESG} Adding user ${USER}"
/bin/adduser -s /bin/bash -G root -D "$USER"
echo "${USER}:password" | /usr/sbin/chpasswd -m > /dev/null
echo -e "${USER}\tALL=NOPASSWD: ALL" >> /etc/sudoers
mount -o loop /mnt/sd${DRIVE}${PARTITION}/*/program-drive-${ARCH}/home "/home/${USER}/" 2> /dev/null
rmdir "/home/${USER}/lost+found/" 2> /dev/null
chmod 770 "/home/${USER}/"
if [ ! "${?}" = '0' ]; then
echo "${MESG} Error mounting home on program drive! 'home' file gone or corrupt."
echo "${RED}SYSTEM BROKEN, PRESS ANY KEY TO SHUTDOWN:"
read -sn 1
poweroff
fi
fi
fi
done
done

CRON Alarm Clock Linux Mint Stops Suddenly

I'm new to linux and I'm learning to script an alarm clock using bash and cron. The script works fine from bash and by double clicking on it, but not from cron; as soon as cron calls the script, the alarm sounds once (one loop, it seems) and then stops without even showing the dialog box (zenity). I'm using Linux Mint 13 Maya XFCE.
Here is my crontab setup
* * * * * /home/x/Documents/MyScripts/Cron/BeepAlarm "Wake Up"
And here is my script
!#/bin/bash
# Initialize Variables
Text=$1
Title="Alarm"
OkLabel="Snooze"
CancelLabel='Shut It Up!'
Icon=/home/x/.icons/actions/48/appointment-new.png # Won't work
SnoozeTimeout=120
AlarmCycles=100
shopt expand_aliases
alias vol='amixer -q -c 0 sset Beep'
vol 25% # Heart friendly
rm -f LoopMode # Just in case =P
# Dialog Box Function
_MsgBox () {
if zenity --question --title="$Title" \
--window-icon=$Icon --ok-label="$OkLabel" \
--cancel-label="$CancelLabel" --text="$Text"
then
echo 1 > LoopMode
else
echo 2 > LoopMode
#kill -TERM `jobs -p` # Won't work
fi
}
# Alarm Loop
while [ $AlarmCycles -gt 0 ]; do
case `cat LoopMode` in
"") # Question Box
echo 0 > LoopMode
_MsgBox &
;;
1) # Snooze
vol 25%
rm -f LoopMode
sleep $SnoozeTimeout
;;
2) # Stop Alarm
break # But don't sleep again
;;
*) # Get on my nerves
beep -r 4 -l 20 -f 2000; sleep 0.5
beep -r 4 -l 20 -f 4000; sleep 0.5
AlarmCycles=$((AlarmCycles-1))
vol 5%+
esac
done
# Housekeeping~
kill $(($!+2)) # I don't know a better way to kill zenity Dialog
vol 25% # =)
rm -f LoopMode
As Rob suggested:
* * * * * env DISPLAY=:0.0 /home/x/Documents/MyScripts/Cron/BeepAlarm "Wake Up"
P.S: Sorry, I didn't pay attention to the formalities

Resources