vncserver bash script error in ubuntu 14.04.4 LTS - linux

I use Ubuntu 14.04.4 LTS and installed vnc4server.
The basic script starting vncdesktops is under /etc/init.d/vncserver which is a bash script. This works fine for all users specified in /etc/vncserver/vncservers.conf with their arguments. But when a user is using csh instead of bash, the vncserver command doesn't work because of a syntax error in the vncserver script. The error occurs in the start() function which I show here.
start() {
. /lib/lsb/init-functions
REQ_USER=$2
echo -n $"Starting $prog: "
ulimit -S -c 0 >/dev/null 2>&1
RETVAL=0
for display in ${VNCSERVERS}
do
export USER="${display##*:}"
if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
echo -n "${display} "
unset BASH_ENV ENV
DISP="${display%%:*}"
export VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
fi
done
}
When I give the command 'sudo service vncserver restart' the vncserver script runs and give me this error for a user using csh.
Starting VNC server: 8:test1 [: No match.
user 'test1' is using csh and its display number is 8. I can see this error is coming from the line
su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
and I understand what it's doing but I don't know why the bracket condition is giving me this error and that only for a user using csh.
Can anyone give me a clue?

I don't know exactly why, but I found my /bin/csh was linked as below.
/bin/csh -> /etc/alternatives/csh
So I installed tcsh (apt-get install tcsh) and made the link like this.
/bin/csh -> /bin/tcsh
Then the problem is gone!
The su command and the command executed by -c option is being processed in the su'ed user's shell which was /etc/alternatives/csh which probably could not handle the [ -f ] condition. and I gues the /etc/alternatives/csh is a kind of default csh linked to when there is no real full fledged csh(like tcsh).

Related

Linux script How to open new terminal and run commands in it

I am writing a script which will take users input and then run following command in new terminal.
I am working on bigger "script", but for testing I created this little version of my problem.
newtermtest.sh
okegreen='\033[92m'
while true
read -p "Input Selection: " mainmenuinput
do
#find the name of instaled terminal then run new window of it and run bash in it !
#on my computer I have instaled x-terminal-emulator: so testing on it ---- NOT WORKING YET
case $mainmenuinput in
"0")
x-terminal-emulator -e "bash -c 'nmap -sV -sC 189.55.23.174 -vvv'"
;;
"1")
path=$(pwd)
x-terminal-emulator -e "bash -c 'cd $path;touch silk.txt'"
;;
*)
break;
esac
done
echo -e "${okegreen}leaving programe..."
I have found that something like x-terminal-emulator -e {command} should work, but when I run it I have the following error and the screen just flashes.
returned message:
QDir::exists: Empty or null file name
Then I would need something to keep the terminal up, I found --noclose or -hold tags, but I can't test it util I find the working command.
Do you have some experiences with opening new terminal from bash script ?
Iam opened for any hints.
Some temporary solution which works now:
x-terminal-emulator -e 'nmap -sV -sC 189.55.23.174 -vvv'
I used just -e to execute command and with nmap it is working now.
I will figure out other commands and I will try to put it in complex script with more types of terminals.
Thanks All for suggestions.

Bash on Windows: script only progresses in -v mode

I have a script which starts like this:
#!/bin/bash
echo "Running on OSTYPE: '$OSTYPE'"
DISTRO=""
CODENAME=""
SUDO=$(command -v sudo 2>/dev/null)
echo foo
When I run it as-is or with bash -x, it stops after the assignment to SUDO, I get as only output
+ DISTRO=
+ CODENAME=
++ command -v sudo
+ SUDO=
When I add -v to the bash invocation to get even more verbose output, the script runs normally and I see my "foo". I'm using bash 4.4.23 as shipped on https://git-scm.com/downloads
What is going wrong on my system and how can I debug this?
This happens because $(command ...) is a subshell.
Instead of set -x or bash -x create a file like follow inside your home folder:
.bash_env
set -x
So the set -x will be applied to any new bash instance and subinstance.

Launching a bash shell from a sudo-ed environment

Apologies for the confusing Question title. I am trying to launch an interactive bash shell from a shell script ( say shel2.sh) which has been launched by a parent script (shel1.sh) in a sudo-ed environment. ( I am creating a guided deployment
script for my software which needs to be installed as super-user , hence the sudo, but may need the user to access the shell. )
Here's shel1.sh
#!/bin/bash
set -x
sudo bash << EOF
echo $?
./shel2.sh
EOF
echo shel1 done
And here's shel2.sh
#!/bin/bash
set -x
bash --norc --verbose --noprofile -i
echo $?
echo done
I expected this to launch an interactive bash shell which waits for my input before returning to shel1.sh. This is what I see:
+ ./shel1.sh
+ sudo bash
0
+ bash --norc --verbose --noprofile -i
bash-4.3# exit
+ echo 0
0
+ echo done
done
+ echo shel1 done
shel1 done
The bash-4.3# calls an exit automatically and quits. Interestingly if I invoke the bash shell with -l (or --login) the automatic entry is logout !
Can someone explain what is happening here ?
When you use a here document, you are tying up the shell's -- and its spawned child processes' -- standard input to the here document input.
You can avoid using a here document in many situations. For example, replace the here document with a single-quoted string.
#!/bin/bash
set -x
sudo bash -c '
# Aside: How is this actually useful?
echo $?
# Spawned script inherits the stdin of "sudo bash"
./shel2.sh'
echo shel1 done
Without more details, it's hard to see where exactly you want to go with this, but most modern Linux platforms have package managers which allow all kinds of hooks for installation, so that you would typically not need to do this sort of thing. Have you looked into that?

How to run a script in background (linux openwrt)?

I have this script:
#!/bin/sh
while [ true ] ; do
urlfile=$( ls /root/wget/wget-download-link.txt | head -n 1 )
dir=$( cat /root/wget/wget-dir.txt )
if [ "$urlfile" = "" ] ; then
sleep 30
continue
fi
url=$( head -n 1 $urlfile )
if [ "$url" = "" ] ; then
mv $urlfile $urlfile.invalid
continue
fi
mv $urlfile $urlfile.busy
wget -b $url -P $dir -o /www/wget.log -c -t 100 -nc
mv $urlfile.busy $urlfile.done
done
The script basically checks for any new URLs at wget-download-link.txt for every 30 seconds and if there's a new URL it'll download it with wget, the problem is that when I try to run this script on Putty like this
/root/wget/wget_download.sh --daemon
it's still running in the foreground, I still can see the terminal output. How do I make it run in the background ?
In OpenWRT there is neither nohup nor screen available by default, so a solution with only builtin commands would be to start a subshell with brackets and put that one in the background with &:
(/root/wget/wget_download.sh >/dev/null 2>&1 )&
you can test this structure easily on your desktop for example with
(notify-send one && sleep 15 && notify-send two)&
... and then close your console before those 15 seconds are over, you will see the commands in the brackets continue execution after closing the console.
The following command will also work:
((/root/wget/wget_download.sh)&)&
This way you don't have to install the 'nohub' command in the tight memory space of the router used for OpenWrt.
I found this somewhere several years ago. It works.
The &at the end of script should be enough, if you see output from the script it means, that stdout and/or stderr is not closed, or not redirect to /dev/null
You can use this answer:
How to redirect all output to /dev/null
I am using openwrt merlin and the only way to get it working was using the crud cron manager[1]. Nohub and screen are not available as solutions.
cru a pinggw "0 * * * * /bin/ping -c 10 -q 192.168.2.254"
works like charm
[1][https://www.cyberciti.biz/faq/how-to-add-cron-job-on-asuswrt-merlin-wifi-router/]
https://openwrt.org/packages/pkgdata/coreutils-nohup
opkg update
opkg install coreutils-nohup
nohup yourscript.sh &
You can use nohup.
nohup yourscript.sh
or
nohup yourscript.sh &
Your script will keep running even if you close your putty session, and all the output will be written to a text file in same directory.
nohup is often used in combination with the nice command to run processes on a lower priority.
nohup nice yourscript.sh &
See: http://en.wikipedia.org/wiki/Nohup
For busybox in Openwrt Merlin system, I got a better solution which combined cru and date command
cru a YOUR_UNIQUE_CRON_NAME "`date -D '%s' +'%M %H %d %m *' -d $(( \`date +%s\`+2*60 ))` YOUR_CMD_HERE"
which add a cron job running 2 minutes later, and only run once.
Inspired by PlagTag's idea.
In another way these code would tried:
ssh admin#192.168.1.1 "/jffs/your_script.sh &"
Simple and without any programs like nohup screen...
(BTW: worked on Asus-Merlin firmware)
Try this:
nohup /root/wget/wget_download.sh >/dev/null 2>&1 &
It will go to the background so when you close your Putty session, it will be still running, and it won't send messages to the terminal.

ts_calibrate fails to run from systemd script

I have a script which is run by systemd on startup. It runs my application, if the user quits the application the script runs ts_calibrate and, when that's done it runs my app again. Worked fine in Angstrom but not on the Beaglebone Black running Debian.
My mita.sh script
while [ 1 ]; do
if [ -e /etc/pointercal ]; then
echo "Pointercal exists"
./mita6 -platform linuxfb -plugin evdevtouch:/dev/input/event1
echo "Quit mita6"
else
echo "No pointercal"
ts_calibrate
echo "Quit ts_calibrate"
./mita6 -platform linuxfb -plugin evdevtouch:/dev/input/event1
fi
echo "Calibrating..."
ts_calibrate
echo "Calibration Done."
sleep 2
done
When the system boots now, my app starts OK but when I exit the app ts_calibrate fails to run. I get the following error:-
ts_open: No such file or directory
If I login in as root, then I can run the script from the command line and everything works as expected.
If, from the command line I enter sudo ts_calibrate then I get the same error.
Can someone help me understand whats going on here?
I can run the script successfully if I ssh in as root but if I then sudo, I don't seem to have the privileges for to run ts_calibrate! systemd also does not seem to have the correct privileges!
Regards,
James.
.
.
.
The systemd service
#!/bin/sh -
[Unit]
Description=My Script
[Service]
USER=root
WorkingDirectory=/home/root
ExecStart=/bin/sh -c 'source /etc/profile ; /home/root/mita.sh'
After=dropbear.service
Type=oneshot
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
.
uname -a
Linux beaglebone 3.8.13-bone67 #1 SMP Wed Sep 24 21:30:03 UTC 2014 armv7l GNU/Linux
The environment variables were not passed, it seems, when ts_calibrate was executed so I had to make sure they were set and passed.
The final working script looks like this:-
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_FBDEVICE=/dev/fb0
export TSLIB_TSDEVICE=/dev/input/event1
while [ 1 ]; do
if [ -e /etc/pointercal ]; then
echo "Pointercal exists"
/home/root/mita6 -platform linuxfb -plugin evdevtouch:/dev/input/event1
echo "Quit mita6"
else
echo "No pointercal"
killall mita6
sudo -E /usr/bin/ts_calibrate
echo "Quit ts_calibrate"
/home/root/mita6 -platform linuxfb -plugin evdevtouch:/dev/input/event1
fi
echo "Calibrating..."
killall mita6
sudo -E /usr/bin/ts_calibrate
echo "Calibration Done."
sleep 2
done

Resources