redirect output of an already running process

Long story short: you have launched your script/program but forgot to redirect the output to a file for later inspection. #!/usr/bin/python3 #sample endless running program that prints to stdout import time,datetime while True: print(datetime.datetime.now().time()) time.sleep(1) Using GNU Debugger you can re-attach to the process, then invoke the creation of a logfile and duplicate the file descriptor to make the system send the data to the new file, instead of the terminal: ...

April 24, 2015 · Andrea Manzini

Sincronizzare una directory tra due server linux

Obiettivo vogliamo mantenere la stessa directory sincronizzata su due server linux. Questo significa che ogni aggiunta/rimozione/modifica di file in questa directory verrà automaticamente riportato sull’altro (salvo conflitti). Diamo per assunto che i due server siano raggiungibili via rete, ma per qualsiasi motivo non sia possibile collegare dello spazio disco condiviso. Implementazione Per raggiungere lo scopo, utilizzeremo il tool: csync2 su entrambi i server (che chiameremo nodo1 e nodo2), installiamo i pacchetti necessari: ...

March 20, 2015 · Andrea Manzini

Linux: get simple I/O statistics per process

I had a trouble with a long process running and wish to know how much I/O this process is doing, so I wrote this quick and dirty python 2.x script: import time,sys,datetime def read_stat(pid): f=open("/proc/%s/io" % pid ,"r") for line in f: if line.startswith('rchar'): rchar=line.split(':')[1] continue if line.startswith('wchar'): wchar=line.split(':')[1] continue f.close() return int(rchar),int(wchar) pid=sys.argv[1] r0,w0 = read_stat(pid) while 1: time.sleep(1) r1,w1 = read_stat(pid) print "%s\t\tr=%s\t\tw=%s" % (datetime.datetime.now().time(),r1-r0,w1-w0) r0,w0=r1,w1 You must give the process PID number as input to the script. In the output you get the read/write throughput of the process in bytes per second, for instance: ...

August 22, 2014 · Andrea Manzini

Compress and encrypt your backups

It’s always recommended to backup your data for safety, but for safety AND security let’s encrypt your backups! to compress and encrypt with ‘mypassword’: tar -Jcf - directory | openssl aes-256-cbc -salt -k mypassword -out backup.tar.xz.aes to decrypt and decompress: openssl aes-256-cbc -d -salt -k mypassword -in backup.tar.xz.aes | tar -xJ -f - Another trick with the tar command is useful for remote backups: tar -zcvfp - /wwwdata | ssh root@remote.server.com "cat > /backup/wwwdata.tar.gz" ...

June 11, 2014 · Andrea Manzini