Bash script. Open new terminal and run command [duplicate] - linux

This question already has answers here:
how do i start commands in new terminals in BASH script
(2 answers)
Closed 19 days ago.
How can I run command in new terminal from bash?
If I run it just from one terminal, mosquitto_sub - blocks the script. xterm -e opens new terminal but my script blocks too...
#!/bin/bash
COUNTER=0
xterm -e mosquitto_sub -h 192.168.1.103 -t test
mosquitto_pub -h 192.168.1.103 -t test -m "Connected"
cd Desktop/ScreenTool/image/
while [ $COUNTER == 0 ]; do
tesseract c.png output
if grep -q Click "/root/Desktop/ScreenTool/image/output.txt"; then
mosquitto_pub -h 192.168.1.103 -t test -m "Rain is here"
echo -en "\007"
fi
cat "/root/Desktop/ScreenTool/image/output.txt"
sleep 3;
done

To execute a command without waiting for it to finish, put it in the background with &.
#!/bin/bash
COUNTER=0
xterm -e mosquitto_sub -h 192.168.1.103 -t test &
mosquitto_pub -h 192.168.1.103 -t test -m "Connected"
cd Desktop/ScreenTool/image/
while [ $COUNTER == 0 ]; do
tesseract c.png output
if grep -q Click "/root/Desktop/ScreenTool/image/output.txt"; then
mosquitto_pub -h 192.168.1.103 -t test -m "Rain is here"
echo -en "\007"
fi
cat "/root/Desktop/ScreenTool/image/output.txt"
sleep 3;
done

Related

I want to create a script that automatically inputs password to ssh client to do reboot on /etc/rc.local

The code manually is:
ssh -fNv -L 3049:localhost:2049 ubuntu#20.115.5.61
Now I have in /etc/rc.local:
#!/bin/bash
remote_host=ubuntu#20.115.5.61
remote_port=3049
local_port=2049
cmd="ssh -fN -R ${remote_port}:localhost:${local_port} ${remote_host}"
while true; do
pgrep -fx "$cmd" >/dev/null 2>&1 || $cmd
sleep 10
done
¿Help me?

How to prperly launch mjpg_streamer when a webcam is pluged in?

I am trying to launch mjpg streamer when a webcam is plugged in.
So far I've been able to detect when the webcam is pluged and I've been able to launch it.
But two issues appear:
first the port doesn't appear to be open
second the process is killed after about 3 minutes
Im running on Ubuntu 18.04.4 LTS
here is what I assume is the culprit file
/etc/udev/rules.d/10-usbmount.rules
#https://stackoverflow.com/questions/20084740/udev-run-program-on-usb-flash-drive-insert
KERNEL=="video[0-9]*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/usr/bin/usbdevinserted"
/usr/bin/usbdevinserted
#!/bin/bash
echo $DEVNAME 2>&1 > /tmp/usbdevinfo
#set 2>&1 >> /tmp/usbdevinfo
if [[ $(pgrep mjpg_streamer) ]];
then
echo "Stoping Mjpg_Streamer" 2>&1 >> /tmp/usbdevinfo;
pkill mjpg_streamer;
echo "mjpg_Streamer stoped" 2>&1 >> /tmp/usbdevinfo;
fi
if [[ $(v4l2-ctl --device=$DEVNAME --all | grep "Video input") ]];
then
echo "launching mjpg_streamer on $DEVNAME" 2>&1 >> /tmp/usbdevinfo;
(mjpg_streamer -i "input_uvc.so -f 15 -r 1080x720 -d $DEVNAME" -o "output_http.so -w /pathToHome/mjpg-streamer/www -p 8080" 2>&1 >> /tmp/usbdevinfo) | at now
fi

Working around sudo in shell script child process

So the reason I am asking this is because I'm running two programs simultaneously that are persistent, on the child process a programm is running that requires sudo rights.
#!/bin/bash
echo "Name the file:"
read filename
while [[ 1 -lt 2 ]]
do
if [ -f /home/max/dump/$filename.eth ]; then
echo "File already exist."
read filename
else
break
fi
done
#Now calling a new terminal for dumping
gnome-terminal --title="tcpdump" -e "sh /home/max/dump/dump.sh $filename.eth"
ping -c 1 0 > /dev/null **Waiting for tcpdump to create file**
#Packet analysis program is being executed
Script dump.sh
#!/bin/bash
filename=$1
echo password | sudo tcpdump -i 2 -s 60000 -w /home/max/dump/$filename -U
host 192.168.3.2
#Sudo still asks me for my password though password is piped into stdin

shell script ssh command not working

I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.
sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
I wrote a script to loop through the list and run this command but I get some errors.
#!/bin/bash
read -p "Enter server list: " file
if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi
When I run the script it does not execute the command.
creating amurug on svr102
Usage: useradd [options] LOGIN
Options:
What is wrong with my ssh crommand in my script?
Try this script:
#!/bin/bash
read -p "Enter server list: " file
if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi
As per man bash:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

ssh tunneled command output to file

I have an old Syno NAS and wish to use the "shred" command to wipe this disks inside. The idea is to let the command run to complete on the box itself without the need of a computer.
So far I have managed...
1) to get the right parameters for 'shred'
* runs in the background using the &
2) get that command to output the progress (-v option) to a file shred.txt
* to see from the file what the progress is
shred -v -f -z -n 2 /dev/hdd 2>&1 | tee /volume1/backup/shred.txt &
3) ssh tunnel the command so I can turn off my laptop while its running
ssh -n -f root#host "sh -c 'nohup /opt/bin/shred -f -z -n 2 /dev/sdd > /dev/null 2>&1 &'"
The problem is that I can't combine 2) and 3)
I tried to combine them like this, but the resulting file remained empty:
ssh -n -f root#host "sh -c 'nohup /opt/bin/shred -f -z -n 2 /dev/sdd 2>&1 | tee /volume1/backup/shred.txt > /dev/null &'"
It might be a case of the NOOBS but I can't figure out how to get this done.
Any suggestions?
Thanks. Vince
Commands sh and tee are not needed in here:
ssh -n root#host 'nohup /opt/bin/shred -f -z -n 2 /dev/sdd 2>&1 >/volume1/backup/shred.txt &' >/dev/null
The final >/dev/null is optional, it will just disregard any greetings from other hosts.
Tried the following command (based on Grzegorz suggestion) and included the opening date stamp and the before mentioned - stupidly forgotten - verbose switch. Last version of the command string:
ssh -n root#host 'date > /volume1/backup/shred_sda.txt; nohup /opt/bin/shred -v -f -z -n 4 /dev/sda 2>&1 >> /volume1/backup/shred_sda.txt # >/dev/null'
The last thing to figure out is how to include the date stamp when the shred command has completed.

Resources