System & Shell commands

  • Run a process detached to the current terminal:
    nohup my_command &
    
  • Get the exit code of the latest runned command:
    echo $?
    
  • Run the last command as root (source):
    sudo !!
    
  • Show the user under which I’m currently logged in:
    whoami
    
  • List cron jobs of the current user:
    crontab -l
    
  • If you have the following error:
    -bash: ./myscript.sh: /bin/bash^M: bad interpreter: No such file or directory
    

    Then the fix consist of removing the bad characters:

    sed -i 's/\r//' ./myscript.sh
    
  • Free up some memory by clearing RAM caches (source):
    sync ; echo 3 > /proc/sys/vm/drop_caches
    
  • Display which distro is running the system (source):
    lsb_release -a
    

    or

    cat /etc/lsb-release
    
  • List of most used commands:
    history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
    
  • Disable a service on Debian/Ubuntu, then re-enable it:
    update-rc.d my-service-name remove
    update-rc.d my-service-name defaults
    
  • Same thing as above but on a RedHat-like system:
    chkconfig sshd --del
    chkconfig sshd --add
    

3 thoughts on “System & Shell commands

  1. Another tip: if you have screen and retty on your computer, you can move an application to a screen by using screen retty $PID. I like it better than nohup because you can keep the interactivity with the process.

  2. @djib: you’re absolutely right ! screen is a really good tool. To be honest playing with it is on my TODO list for what… 5 years ? It’s just that because I know nohup exists I don’t have any strong incensitive to learn screen;)

  3. Pingback: All my command lines | Kevin Deldycke

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>