Posts Tagged ‘Linux’

Detach process from parent shell

When using UNIX or similar systems via CLI, you typically log in a shell as a single running process, spawned by another process such as any ssh-daemon if you are connecting remotely or by the gnome-terminal process if you are in a gnome-desktop environment, and so on ( by the way, you can check what is your shell spawned from with the pstree utility ). Anyway, from this shell you will probably provide commands, that wil launch programs, that will be put into concrete form by the OS spawning new processes ( actually, forking the current process and calling something like execve ).
While you can play with the launched processes with some jobs control techniques, sometimes you just want to start some programs that should be completed independently from the fate of your shell (e.g. you want to log out, or just to close your terminal emulator, or just your shell somehow dies). That means, in other words, that you may want to run something and then log out letting the machine do the job while you can go performing more pleasant tasks :) .
This is also often seen as " turning a process into a daemon " , even if it’s not strictly correct.
This can be easily achieved with the nohup POSIX command that will instruct your process to ignore the SIGHUP signal if your shell dies (e.g. when you log out). To use it, you can just append the command you want to provide after the nohup invocation such as:

nohup ./my_time_consuming_script.sh and its args

By the way, if you use the bash shell, you can get the same result using the disown builtin command, that can remove jobs from the job table, or to mark jobs so that SIGHUP is not sent to them if the parent shell receives it. The syntax is:

disown [-ar] [-h] [jobspec ...]

with options meaning:

  • -h do not remove from job table
  • -a apply to all jobs
  • -r apply to running jobs only

That’s it, let the machine do the jobs and go sunbathing ;)


SSH login without password

Ok, this can be found pretty everywhere on the internet, but on the n-th time someone asked me how to use management scripts that do stuffs via SSH without have to enter one or more password, I need to write it here so that next time I’ll have no remorse in saying “go read it online, on my website”.

Well, here’s the story: you can connect via ssh to other hosts without entering your password as long as you can prove that you are authorized to login with that identity. This is achieved by placing a public key on the host you want to connect to, so that when you try to login an authentication handshake is prompted to your machine that is then supposed to own the corresponding private key and thus to be able to complete the handshake.

This is what happend behind, you won’t see anything of this during your ssh login. But in order to make this mechanism work, you have to place that public key on the host you want to connect to. Probably for this purpose you may want to use your public RSA key generated to use ssh, that you can find in ~/.ssh/id_rsa.pub where ‘~’ is your home folder ( please be careful not mismatching this with ~/.ssh/id_rsa that is your private key: instead take care of this file by setting proper permissions and/or encryption because this file represent your identity and if leaked, anyone can access any machine where your public key is placed to consider you an “authorized” user ).

The place where ssh daemon looks for authorized keys when someone try to connect to the system as the given user is, with very small imagination, the ~user/.ssh/authorized_keys file, where ~user is the home of the user you are trying to login as e.g. with ssh user@whaterver-host.com. So basically what you have to do is append your own ~/.ssh/id_rsa.pub to the remote ~user/.ssh/authorized_keys. Here is a command you can use ( you will have to prompt the password two last times :P ).

ssh user@whaterver-host.com 'if [ ! -d ~/.ssh ];then mkdir ~/.ssh; fi'; 
cat ~/.ssh/id_rsa.pub | \
  ssh user@whaterver-host.com 'cat >> ~/.ssh/authorized_keys'

Now you will be able to login to whaterver-host.com just with ‘ ssh user@whaterver-host.com ‘ without being prompted for any password, even from many machines if you keep your id_rsa pair with you across them.( But remember, keep it safe! )
Cheers!


Advanced Shell Scripting

Yesterday I delivered a lesson for the advanced Linux course arranged by LUG Roma3 at Roma Tre University on voluntary base. Here are the slides I sketched out for this occasion.




It’s not really “advanced” stuff, but sounds just better than “basics +1″ ;)


Change keyboard layout in Linux

Keybord gth There are several ways to change the keyboard layout in linux.
First, you can load a given keyboard translation tables with :

loadkeys <tablecode>

Alternatively, if you are in a X11 env, you can set the keyboard map using the X Keyboard Extension with:

setxkbmap <kbmapcode>

But if you want to permanently change the console keyboard layout, e.g. in Debian, you can reconfigure console-data and use the wizard to select the desired layout with:

dpkg-reconfigure console-data

Web app backup simple shell script

Backup Recently, the unofficial discussion board for students of my university department went down because of a misunderstanding with their hosting provider who also reassigned their machine to another customer wiping out all of their data. Now they restore the board but the last backup was dated back to last year so lots of data have been lost. Anyway, when one of the admins announced on facebook the board was up again, i ironically commented writing a small shell script that can be used to backup a simple mysql-based web application such as a discussion board or a CMS installation such as drupal, joomla, wp or whatever… But then i thought this happend more frequently than expected to people i know, so i decided to post that script here :)

#!/bin/bash
#-----------------------------------
# Web App. Dumb Backup Script
# http://www.n0on3.net
#-----------------------------------
$user='your-username-here'
$server='your-domain-name-here'
$appname='your-webapp-name-here'
$apppath='your-webapp-path-here'
$mysqluser='your-mysql-user-here'
$mysqlpassword='your-mysql-pwd-here'
$dbname='your-mysql-db-name-here'
#-----------------------------------
d=`date +'%d-%m-%y'`
ssh $user@$server "tar cjvf backup-$appname-$d-www.tar.bz2 $apppath"
ssh $user@$server "mysqldump -u $mysqluser
                  --password=$mysqlpassword $dbname
                  &gt; backup-$appname-$d-db.sql"
scp $user@$server:$HOME/$user/backup-$appname-$d-www.tar.bz2 \
                  backup-$appname-$d-www.tar.bz2
scp $user@$server:$HOME/$user/backup-$appname-$d-db.sql \
                  backup-$appname-$d-db.sql
ssh $user@$server 'rm backup-$appname-$d-*'

Please notice that here you are using ssh login without password, that means you have to append your client machine public rsa id to your server authorized keys file.
But more important, here you are writing your database password in plaintext because the script must use it, so if you keep such a script on your client machine remember to encrypt it or to take any proper precaution ;)


Download YouTube videos with a shell script

In my last post I wrote about extracting audio from in-streaming-format videos, that people often may get from content streaming services like eg. YouTube. As said, those videos may be downloaded in several ways that may also be different depending on the hosting service. A pretty effective way is using some plugin for your browser that shows the URL of the source the flash/html5 player download the video from, and allow you to get the content from that. Anyway, just for fun, here is a small shell script that can be used to download videos contents from youtube.

#!/bin/bash
#
################################################################
# --- YouTube Downloader ------------------------------------- #
# --- http://www.n0on3.net ----------------------------------- #
################################################################
#
which wget &>/dev/null; 
if [ $? -ne 0 ];then echo "> Please install wget =)!"; exit; fi
if [ $# -ne 1 ];then 
	read -p "> Enter Youtube video ID: " VID
else	VID=$1; fi
read -p "> Do you want me to try to get the HD version ? [y|N]: " HD
case $HD in Y|y|yes) FMT=22;; N|n|*)FMT=18;; esac
OK=1; PAGE=`tempfile`
wget -q -O $PAGE "http://www.youtube.com/watch?v=$VID"
SIG=`cat $PAGE | egrep -o "\"t\": \"[^\"]+\"" | \
     sed 's/\"//g' | awk '{print $2}'`
URL="http://www.youtube.com/get_video?fmt=$FMT&video_id=$VID&t=$SIG"
TITLE=`cat $PAGE | grep VIDEO_TITLE | awk '{$1="";print $0}' `
TITLE=$(echo $TITLE | sed 's/.\(.*\)../\1/' | sed 's/\\//g')
read -p "> Is the title \" $TITLE \" right for output file ? [Y|n]: " TOK
case $TOK in 	n|N|no) read -p "> Enter filename: " TITLE ;;
		Y|y|yes|*) ;; 
esac; echo "> Saving file to $TITLE.mp4 ... "
wget -q -O "$TITLE.mp4" $URL &>/dev/null
if [ $? -ne 0 ];then echo "> Download failed!";
	OK=0; rm -fr "$TITLE.mp4";
	if [ $FMT == 22 ];then 
		echo "> Maybe the HD version isn't available.."
		FMT=18; echo "> Downloading the non-HD version ..."
	URL="http://www.youtube.com/get_video?fmt=$FMT&video_id=$VID&t=$SIG"; 
	wget -q -O "$TITLE.mp4" $URL &>/dev/null; 
		if [ $? -ne 0 ];then 
			echo "> Download failed!"; rm -fr "$TITLE.mp4";
		else OK=1; fi; 
	fi
fi
if [ $OK -eq 1 ]; then echo "> Successfully Downloaded to $TITLE.mp4 !"
fi;
#

To be said, youtube term of service, at point 6.C, states using YouTube you accept not to access the contents with any technology but the YouTube website player or other stuff explicitly authorized by YouTube. ( That means, yeah, you should really not use this u.u” ).


Return top

About me