bash/

rygel: Disable inactivity timeout when streaming video

This script demonstrates how to disable system inactivity timeout in Gnome when running Rygel and restore it to the original value after exiting:

#!/bin/bash

# Save current inactivity timeout
TIMEOUT=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout)
echo "Current timeout: $TIMEOUT"

# Execute commands on Ctrl + C
function ctrl_c() {
  echo "Exiting rygel..."
  gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout $TIMEOUT
  echo "Inactivity timeout restored to $TIMEOUT. Bye!"
  sleep 2
}

# Set the trap for Ctrl+C
trap ctrl_c INT

# Disable the inactivity timeout
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 0

# Briefly start rygel to allow it to add new files
timeout 2s rygel

# Run command to fix filenames
rygel-titlefix

# Actually run rygel
rygel

| 17 Jul 2023

Wait until DNS propagates

until [[ -n $(dig new.mydomain.com A +short @1.1.1.1) ]]; do echo "waiting..." && sleep 5; done

-n flag stands for non-empty string

| 22 Dec 2021

Clear file from leading new line and etc

cat file | tr -d [$'\t\r\n'] > new_file
| 26 Feb 2021

Add exit code to the command line

Some examples to add exit code to command line prompt in bash (put it into ~/.bashrc):

export PS1="\[\033[01;31m\]\$([ \$? == 0 ] || echo \"!\$? \" )\[\033[00m\]\[\033[01;32m\]\t \[\033[01;34m\]\w\[\033[00m\]\[\033[1;32m\]\n\$ \[\033[m\]"

Add to the default PS1 in CentOS:

export PS1="\[\033[01;31m\]\${?##0}\[\033[00m\][\u@\h \W]\\$"
| 11 Jan 2021

Log executed command

14:27 $ (set -x; sleep 1 && sleep 2)
+ sleep 1
+ sleep 2
| 1 Jan 2020

Wait until able to connect

while ! nc -w5 -z 10.0.0.100 22; do echo "hello"; done
| 1 Jan 2020

How to check what hides behind an alias

To check what command is assigned to the alias use type command:

$ type gl
gl is aliased to `git log --oneline`
| 1 Jan 2020

Run command as a different user

Command runuser allows to run a command as a different user. You must be root to be able to run that command:

sudo runuser -l vagrant -c "GH=1 env"
| 1 Oct 2017

Get size of the subfolders

Use this command to print size of the subfolders:

du -sh *

If you want to exclude Permission denied errors, for example:

du -sh * 2>/dev/null
| 1 May 2017

Print exit code of the last command

To print exit code of the last command type:

echo $?
| 1 Mar 2017