Ubuntu sound/network/USB trouble after suspend , how to restart - audio

I had a lengthy problem with Ubuntu 14.04 with Lenovo S20-30: after resuming suspended session some things break:
USB stops recognizing devices
due to this the webcam and bluetooth stop working
NetworkManager goes to sleep and no internet connections are made
Sound stops or hangs up in false "Headphones" mode,
or "Dummy" output is shown in Settings->Sound

This is a summary of many different answers (from the stack and others) on this topic which worked consistently for me:
to restart USB run in terminal as root e.g. in a scipt:
#!/bin/bash
for I in $(ls /sys/bus/pci/drivers/xhci_hcd/|grep : ) ; do
echo $I
sudo echo $I > /sys/bus/pci/drivers/xhci_hcd/unbind
sudo echo $I > /sys/bus/pci/drivers/xhci_hcd/bind
done
to wake up (resume) NetworkManager as normal user
#!/bin/bash
nmcli nm sleep false
to restart the sound as root
#!/bin/bash
pulseaudio -k ; sudo modprobe -fr snd_hda_intel; sudo modprobe snd-hda-intel
this solves the common message which came up upon restart:
modprobe: FATAL: Module snd_hda_intel is in use.

Adapting the answer by IljaBek into an automate one. Place the following script in a new file called:
/etc/pm/sleep.d/20_usb_unbind_bind
#!/bin/sh
# Action script to ubind then bind USB devices after sleep
#
case "${1}" in
hibernate)
# nothing
;;
resume|thaw)
for I in $(ls /sys/bus/pci/drivers/xhci_hcd/|grep : ) ; do
echo $I > /sys/bus/pci/drivers/xhci_hcd/unbind
echo $I > /sys/bus/pci/drivers/xhci_hcd/bind
done
;;
esac
Make the file executable with sudo chmod 755 /etc/pm/sleep.d/20_usb_unbind_bind

Related

Issue in start of vncserver through cronjob

Following script I use to start vnc,
cd $HOME; vncserver -kill :12
cd $HOME; vncserver :12 -geometry 1280x1024 -name myvnc --alwaysshared
It works when I run from terminal, but It gives error when run from cronjob
Screenshot of vnc when start from cron
Machine configuration
Operating System: Red Hat Enterprise Linux Server 7.6 (Maipo)
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.6:GA:server
Kernel: Linux 3.10.0-957.el7.x86_64
Architecture: x86-64
Machine was up for last 141 days, after reboot of machine the above issue gets resolved. Can any one tell me how to figure out what cause this issue to arise?
Update:
Still, issues arise within every 3-4 days after reboot.
Suspects are as follows:
cron job user has been changed to/from root, and so $HOME has changed accordingly.
Try an "&& wait" to make sure the -kill has finished before issuing the next command.
Do you have a MAX sessions set? Has it been reached? And X may be the issue.
Curious, what is the system response when you do the following? Is the previous session still running? Did the -kill command complete? Does it always?
vncserver -kill :12
ps -fU "$USER" | grep vnc # or try: ps -e | grep vnc
May I suggest the following to aid the debug process...
#!/bin/sh
cd "$HOME" || exit 1 # a good habit, don't assume the landing.
echo "$HOME" # confirm that the value is what you expect.
vncserver -kill :12 && wait # wait for the kill to complete before restarting
vncserver :12 -geometry 1289x1024 -name myvnc --alwaysshared
exit 0
Perhaps even better:
#!/bin/sh
cd "$HOME" || exit 1
if ! vncserver -kill :12 ; then
vncserver :12 -geometry 1289x1024 -name myvnc --alwaysshared
else
echo "Error: vncserver 12 Process NOT KILLED, $? " # $? will give you the return/exit value of previous
fi
exit 0
Notes:
You didn't say what shell you are using but you can substitute #!/bin/bash in place of #!/bin/sh
You didn't paste the error statement.
Find a hack,
If I create vnc from root user then it works perfectly.
In cron you can add entry as below
su - myuser -c "cd $HOME; vncserver -kill :12; vncserver :12 -geometry 1280x1024 -name myvnc --alwaysshared "

How to convert sysvinit script to systemd on Manjaro

First, please don't consider this post as a systemd review or critic, but only and simply as a request for help.
Since I've not been able to find a solution to this problem with the systemd documentation, I've this question not solved for almost a year and a half that never ever received any answer.
So, here is the context:
I've a program (/opt/myprog) that can be sarted as a deamon service at boot time.
When using previous Debian, LMDE, Mint or Ubuntu OSes, I used SysVinit with the following script (myprog.sh within the /etc/init.d folder):
MYPROG_PATH=/opt/myprog_64
NAME="myprog"
START="-d"
STOP="-k"
TEST=""
VERSION="-v"
SCRIPTNAME=/etc/init.d/$NAME
STARTMESG="\nStarting $NAME in deamon mode.\n"
UPMESG="\$NAME is running.\n"
DOWNMESG="\$NAME is not running!\n"
TESTMESG="\nStarting NAME in client mode.\nHit Ctrl+C (or close the terminal) to stop mprog.\n"
STATUS=`pidof $NAME`
# Exit if myprog is not installed
[ -x "$MYPROG_PATH/$NAME" ] || exit 0
case "$1" in
start)
sleep 3
echo $STARTMESG
cd $MYPROG_PATH
./$NAME $START
;;
stop)
cd $MYPROG_PATH
./$NAME $STOP
;;
status)
if [ "$STATUS" > 0 ] ; then
echo $UPMESG
else
echo $DOWNMESG
fi
;;
restart)
cd $MYPROG_PATH
./$NAME $STOP
echo $STARTMESG
./$NAME $START
;;
version)
cd $MYPROG_PATH
./$NAME $VERSION
;;
test)
cd $MYPROG_PATH
echo $TESTMESG
./$NAME
;;
*)
echo "Usage: $SCRIPTNAME {start|status|restart|stop|version|test}" >&2
exit 3
;;
esac
:
Now, since it's obvious that systemd will be widely adopted to replace SysVinit including with future Debian, Mint and Ubuntu distros as it's with CentOS, Fedroa or Ach and Manjaro, I've tried to adapt my sysVinit script to systemd with the following script that works but is too limited (myprog.service):
Description=myprog
ConditionFileExecutable=/opt/myprog_64
After=NetworkManager.service
[Service]
Type=oneshot
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/opt/myprog -d
ExecStop=/opt/myprog -k
ExecRestart=/opt/myprog-k : /opt/myprog -d
TimeoutSec=0
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
However, as systemd is advertised as compatible and more flexible than SysVinit, could anyone show me how to add the three following equivalent switches (status, test and version) that I have defined in the myprog.sh sysVinit script without responding with the classic and inelegant answer: "man is your friend" ?
/opt/myprog status to display the myprog status (i.e. running or not)
/opt/myprog test to start myprog not as a deamon
/opt/myprog version to display the release of myprog
Thank you in advance fo your time and help.
systemd does not support custom implementation of arguments to systemctl.
So systemctl status myprog will show the results based the execution of Exec* settings.
systemctl show myprog uses the Description so you can use a version in your description if desired.
If you wan't to run your program not as a daemon, then you can start it outside of systemd.

How to - multiple dropbox instances in Linux?

After looking around online it seems pretty easy to have multiple dropbox accounts running. All you have to do is change an environmental variable and then run dropbox. However, I've tried editing the .desktop file (see .desktop file specification) so the Exec line is changed from this:
Exec=dropbox start -i
which is the default, to this:
Exec=env "HOME\=/home/reg/.dropbox-alt" dropbox start -i
which from everything I have read should work. I've also tried all the variations of escaping and quoting like:
Exec=env HOME\=/home/reg/.dropbox-alt dropbox start -i
Exec=env "HOME=/home/reg/.dropbox-alt" dropbox start -i
Exec=env HOME=/home/reg/.dropbox-alt dropbox start -i
and nothing seems to launch dropbox. However if I try the same line in bash it tries to launch but falls short but that's only because dropbox is looking for a GUI. That being the case I would have thought that doing the above in the .desktop file would work but I get nothing at all happening.
I'm doing this without any dropbox instances running already so it cannot be that dropbox is looking for other instances and stopping itself from loading another instance.
If I try this in the .desktop file:
Exec=env dropbox start -i
It will launch dropbox but now it's the default instance which has no benefit.
Can anyone tell me what I'm missing to make this work?
Open a terminal and paste the following commands:
$ mkdir "$HOME"/.dropbox-alt
$ ln -s "$HOME/.Xauthority" "$HOME/.dropbox-alt/"
$ HOME="$HOME/.dropbox-alt"
$ /home/$USER/.dropbox-dist/dropboxd
Dropbox setup wizard window will appear. Finish the setup similarly as described in Method -1
start Dropbox from terminal
$ /home/$USER/.dropbox-dist/dropboxd
start Alternate-Dropbox from terminal
$ HOME="$HOME/.dropbox-alt" && /home/$USER/.dropbox-dist/dropboxd
Note:
You can create a small script with the above commands to start Dropbox.
One can put the script at startup. Don't forget to give the script execution permission.
chmod +x /path/to/script
I have tested the second method. Hope it will be useful.
#!/bin/bash
HOME_DIR=$HOME
DROPBOXES=("$HOME/.dropboxes/personal" "$HOME/.dropboxes/business")
function start_dropbox() {
HOME=$HOME_DIR
local flag
local home_dir
local OPTIND;
local verbose=0
local wait=0
while getopts p:vw opt; do
case $opt in
p) home_dir="$(echo $OPTARG | sed 's:/*$::')/" ;;
v) verbose=1 ;;
w) wait=1 ;;
*) ;;
esac
done
shift $((OPTIND-1))
# Test if the process is already running
local pid=$(ps aux|grep "${home_dir}.dropbox-dist"|grep -v 'grep'|tr -s ' '| cut -d' ' -f 2)
if [ -n "$pid" ]
then
echo "Process already running with home dir. of: $home_dir"
return 8 # Process already running
fi
# Create home directory if it doesn't exist
if [ ! -e "$home_dir" ]
then
if mkdir -p "$home_dir";
then
echo "Created directory: $home_dir"
else
echo "Failed to create directory: $home_dir"
return 9 # Failed
fi
fi
# Set up so works with GUI from command line
xauthority="${home_dir}.Xauthority"
if [ ! -e "$xauthority" ]
then
ln -s "$HOME/.Xauthority" "$xauthority"
fi
HOME="$home_dir"
# Start the dropbox daemon
if [[ $verbose -gt 0 ]]; then
echo '~/.dropbox-dist/dropboxd & '$home_dir
fi
~/.dropbox-dist/dropboxd &
if [[ $wait -eq 0 ]]; then
sleep 2 # Give each instance time to startup completely before starting another one
else
read -n 1 -s -p 'Press any key to continue.'
echo
fi
}
function start_dropboxes() {
local dropbox
for dropbox in "${DROPBOXES[#]}"
do
start_dropbox $# -p "$dropbox"
done
}
#
# For testing & setup we can choose just one to startup
#
while getopts f:wv opt; do
case $opt in
f) start_dropbox -p "${DROPBOXES[$OPTARG]}" # NOTE: bash array indexes start at 0.
exit ;;
*) ;;
esac
done
OPTIND=1
start_dropboxes $#
Not being able to install multiple instances of same software on a single machine is a typical case of what is called as Software Conflict where two such instances would compete for resources such as memory, peripheral device, register, network port, etc.
However, we could use a container-based virtualization technology called as Docker to run multiple instances of a software on the same machine in loosely isolated environments called as Containers.
The best part of this solution is that it would work on any platform, as containers are meant to portable.
I recently wrote a blog on it, explaining steps to containerise dropbox instances using docker.

Start gpsd service at reboot

I am using a GPS hat from adafruit.
According to the document
Start gpsd and direct it to use HW UART. Simply entering the following
command:
sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
While this does in fact work, I am trying to find a way to automatically call this on a reboot. I've tried putting it in a .py file and calling it when the machine restarts in a cronjob but that doesn't work. (Invalid Syntax). Hoping I could be assisted in accomplishing this.
Thank you
The fastest and easiest way is to put the above command in /etc/rc.local file (without sudo!). This is a shell script invoked on boot.
A more proper way of doing this is to create a service file into /etc/init.d directory. To start see any simple file into that directory, copy and modify it and make sure is executable. Basic (untested) example:
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: gpsd
# Required-Start:
# Required-Stop:
# Default-Start: 1 2 3 4 5
# Default-Stop:
# Short-Description: Run my GPSd
### END INIT INFO
#
case "$1" in
start)
gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
;;
stop)
killall -KILL gpsd
;;
restart|force-reload)
killall -KILL gpsd
sleep 3
gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
;;
*) echo "Usage: $0 {start|stop|restart|force-reload}" >&2; exit 1 ;;
esac
Once you have that make sure it is enabled on boot, so your system will automatically call service gpsd start. This is done with the update-rc.d command on Debian-base distros and systemctl on RHEL.
If you let us know your linux distro we can be more specific.

how to create a script for checking if ssh is running and if not restart it

I'm running a headless system with a raspberry pi and after a while of not connecting via ssh the system will stop responding to ssh, it is not the Wi-Fi dongle falling asleep, I have checked, seeing I have a piglow running piglow-sysmon, and the part of the pi glow that monitors network activity does show activity when the pi stops responding to ssh. I found a nice script for checking if Wi-Fi is up and if not restart it, although im not that great with bash scripting, and cannot figure out how or if i can mod it to work with ssh instead of Wi-Fi, if anyone can help me mod it, or provide a small quick one, I'm using cron to run it (once I can get it modded) every few minutes
here the script I'm trying to mod
#!/bin/bash
LOGFILE=/home/pi/network-monitor.log
if ifconfig wlan0 | grep -q "inet addr:" ;
then
echo "$(date "+%m %d %Y %T") : Wifi OK" >> $LOGFILE
else
echo "$(date "+%m %d %Y %T") : Wifi connection down! Attempting reconnection." >> $LOGFILE
ifup --force wlan0
OUT=$? #save exit status of last command to decide what to do next
if [ $OUT -eq 0 ] ; then
STATE=$(ifconfig wlan0 | grep "inet addr:")
echo "$(date "+%m %d %Y %T") : Network connection reset. Current state is" $STATE >> $LOGFILE
else
echo "$(date "+%m %d %Y %T") : Failed to reset wifi connection" >> $LOGFILE
fi
fi
Try the following script. It makes a few assumptions:
1) Your account has its own ssh key in the authorized_keys file, so that "ssh localhost" essentially just gives you another shell, without prompting for a password
2) If the ssh command does not complete in three seconds, it would be safe to assume that the ssh daemon is up, but stuck for some reason:
#! /bin/bash
ssh localhost /bin/true &
sleep 3; kill -9 $!
if wait $!
then
echo Up
else
echo Down
fi
A bit crude, but should be effective. It's up to you to figure out how to restart the ssh service in an optimum way, here. Fill in the blanks.
You may also want to discard all standard error here, as it'll likely to have some unimportant noise...
If, on the other hand, this script reports that the ssh service is running, but you still can't connect from the outside, the problem is not the ssh service, but it lies elsewhere, so it's back to the drawing board for you.

Resources