Monday, 17 September 2012

Dead Man's Switch on Linux, Part 1: Basic bash

I've always liked the idea of a dead man's switch. It's a partial fix for the 'what happens if I get hit by a bus' problem: how would you give your important account details to your family? How can you get a last minute message to your loved ones? Most importantly, who will delete your porn?

Traditionally, they work with the assistance of a third party. They've been used to launch nukes. Wikileaks used it to cause shenanigans. And you can sign up for some web-based solutions that will send pre-recorded emails for you when you show signs of being dead.

Of course, you have no control over that. the whole site might go down without you realising, rendering your efforts useless. So here's the same basic principle, implemented on your own software in bash script.

If you just want the script, skip to the end. It does things like this:

The Simplest Script Of All

We need three basic components to make this work:
  1. The main script file. When run, it checks to see if the timer has expired. If it has, it performs pre-set actions (like sending email)
  2. A method to 'reset' the timer. A one-line script file will work fine here.
  3. A way to trigger the first script to run. We'll use cron.
We also need a way to check whether we've hit the time limit. To keep things simple, I'm going to use the 'modified' time on the main script file. When it runs, it'll compare when it was last modified against the current time to act as the timer. To reset, you just need to run 'touch' to change the modified time. Easy, right? 

So here's the main script, set to do the most important task of all: Delete Your Porn.

#! /bin/bash
# Simple Dead Man's Script

timelimit=30 #Number of days to wait without update

let timelimit=$timelimit*60*60*24 #turn that into seconds
lastaccessed=$(stat -c %Y $BASH_SOURCE)
timenow=$(date +%s)
let timeleft=($timenow-$lastaccessed)
if [ $timeleft -gt $timelimit ]
    rm -rf $HOME/myporndirectory

Here's the 'reset' script, saved somewhere on your desktop:

#! /bin/bash

touch pathto/

And here's the line you have to add to cron (crontab -e to get there):
0 0 * * * pathto/

Simple, right? make the scripts executable, and if you ever go 30 days without running the reset script it'll delete all your porn. Careful with your holidays.
It can do anything that can be done from the command line. with minimal trouble it can ftp files around, modify/upload entire websites, start torrents, or just update twitter with what you had for lunch the other day. I want to use it to send an email.

The Slightly Less Simple Script

Obviously I took a simple concept and made it both unnecessarily complex and annoyingly basic. It now has a setup option that should work most of the time, and a 'test' option that emails yourself and makes sure it works.

Boring Notes:
  • The setup option assumes a generic install - that you have crontab access, that you have a home directory, etc. It will fail horribly if run as root.
  • The setup script creates a .dmswitch/ folder in your home dir. Inside will be the check script, the message file, and an attachment directory. The message file is plain text and as long as you need.
  • It also adds a file to your desktop. each time you run this, it resets the counter.
  • The setup is optional. If you manually move the files where you like and change the variables, it'll work just fine.
  • I'm using sendemail to do the email sending since it plays nicely with gmail. You can use any other method you like. If you use the script unmodified you'll need to install sendemail.
  • Any files in the attachment directory will be attached to the email. No spaces in filenames though, because I am lazy.

Quick Install:
On debian/ubuntu, it takes four lines and some variable editing.
  • sudo apt-get install sendemail libio-socket-ssl-perl
  • gedit (or nano, vim, emacs, etc.)
  • paste in the script, and edit the email account details to your own. Save.
  • chmod +x
  • ./ setup
Test it by running .dmswitch/ test . If the email settings are correct, you should have a test email arrive shortly.

  • rm -rf .dmswitch/
  • crontab -e, remove the entry pointing to the script.
Finally, The Script itself.
# Basic Dead Man's Switch v1.0
# Options:
# 1) dmswitch setup
#    sets up the script in your home dir with some default settings.
#    best to set it up manually or automatically rather than a mix of the two.
# 2) dmswitch test
#    sends a test email to the itself.
# 3) dmswitch reset
#    'checks in' with the dead man switch and resets the counter to zero
#    Just does the same thing as touching the scriptfile.
# 4) dmswitch
#    default. if the time has expired, IT WILL SEND AN EMAIL.

#some running variables are based on the setup vars
croncommand="0 0 * * * "$setupdir"/"$checkscript #cron line for how often it checks expiry. Default is daily.

#make sure you change the email settings. 
dmdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
timelimit=30 #Number of days to wait without update
emailto="" #The target address
emailfrom="" #The account you send with
emailusername="" #The username for your email account
emailpass="myemailpassword" #password
emailsubject="Automated email from Dead Man's Switch."
message=$dmdir"/"$setupmessage # the text file you want your message stored in
attachmentdir=$dmdir"/"$setupattachmentdir # put any attachments you want to include here

#checks if the time has run out. Does the maths in unix time.
function checkifexpired() {
    let timelimit=$timelimit*60*60*24 
    lastaccessed=$(stat -c %Y $BASH_SOURCE)
    timenow=$(date +%s)
    let timeleft=($timenow-$lastaccessed)
    if [ $timeleft -gt $timelimit ]
    then expired=1

#sends an email. replace sendemail with whatever program you prefer.
function sendemail() {
    attachmentlist=$(ls $attachmentdir)
    cd $attachmentdir
    sendEmail -f $emailfrom -t $emailto -u $emailsubject -s $emailserver":"$emailport -xu $emailusername -xp $emailpass -a $attachmentlist -o message-file=$message

#sets up a directory to run from and creates the necessary files.
function setupdm() {
    mkdir -p $setupdir
    cp $BASH_SOURCE $setupdir"/"$checkscript 
    cd $setupdir
    chmod +x $checkscript
    mkdir $setupattachmentdir
    touch $setupmessage
    echo "If you can read this, I'm dead or arrested or something" >>$setupmessage

    #append cron job to existing cron file
    (crontab -l; echo "$croncommand" ) | crontab
    #setup reset script on desktop
 touch $resetscript
    echo "#! /bin/bash" >>$resetscript
    echo "checkfile="$setupdir"/"$checkscript >>$resetscript
    echo "touch $""checkfile" >>$resetscript
    chmod +x $resetscript
    echo "setup complete"

#main script starts here


if [ "$1" == "setup" ]
elif [ "$1" == "test" ]
    #send test email to yourself
    emailsubject="TEST: "$emailsubject
elif [ "$1" == "reset" ]
elif [ $expired -eq 1 ]
    #send the email, and disable the script from running
    chmod -x $BASH_SOURCE
else exit

Go wild.
Part two will cover the fact that not everybody leaves their desktop on for months at a time and suggest several overly-elaborate ways to trigger the reset while running it on a remote machine.


  1. dongtam
    mu private
    tim phong tro
    nhac san cuc manh
    tổng đài tư vấn luật
    văn phòng luật
    tổng đài tư vấn pháp luật
    thành lập công ty
    chém gió
    trung tâm ngoại ngữ

    - Đội trưởng, lần này ta nghe nói Hổ Quyết lão tổ đã chết, có thật hay không, thực lực của Hổ Quyết lão tổ không chênh lệch lắm so với Hổ Mãnh lão tổ.

    - Trước kia ta nghe nói Hắc Ám Thần Điện hai người tới đây, Viễn Cổ Hổ Tộc sắp xếp không ít nữ nhân hầu hạ, kết quả những nữ nhân đó bị chà đạp không còn hình dạng gì nữa, ta thấy Hắc Ám Thần Điện kia tuyệt đối không phải là thứ gì tốt được rồi, các ngươi đừng nói chuyện nữa.

    Đội trưởng kia cất tiếng trách mắng:

    - Không gian thông đạo lại có người xuất hiện, ta đoán chừng quân giao chiến với Tứ Đại Nhân Tộc đã trở về. Chuyện trong tộc chúng ta không có tư cách bình luận, miễn cho sau này bị xử phạt.

    Ở bên trong Hạo Thiên Tháp Nhạc Thành từ từ thở ra một ngụm trọc khí, hắn mở to hai mắt, ánh mắt của hắn thâm thúy nhìn Hạo Thiên Tháp, sắc mặt của hắn trở nên lạnh lẽo.

  2. This was very helpful, thank you.

  3. اليك خدمات منزلية متقدمة تساعدنا علي الارتقاء بشركة ركن البيت في مدينة الدمام من خلال اعمالها المتميزة التي تقدمها في مجال النظافة والقضاء علي الحشرات فخدماتنا متنوعة مثل شركة تنظيف بالدمام التي تكون متخصصة في اعمال النظافة العامة التي تتمثل في المنشأت الكبيرة والصغيرة بجميع انواع واليك خدمة متميزة ايضا وهي شركة تنظيف فلل بالدمام التي تكون لديها كل الامكانيات المثالية التي تساعدنا في تنفيذ خدمات نظافة الفلل المستعملة والمفروشة بواسطة فريق فني لديه كل الامكانيات التي تساعده في تقديم ذلك ولديك خدمة ايضا نظافة الشقق بواسطة شركة تنظيف شقق بالدمام وعمل التنظيف اللازم لها بواسطة فريق لديه اعمال التميز والتنفيذ في تقديم الخدمات المخصصة في اعمال النظافة واليك خدمة مثالية نقدمها في القضاء علي الحشرات الصغيرة بواسطة مبيدات طبيعة تساعدنا في ذلك فتعاملك مع شركة مكافحة حشرات بالدمام لديها امكانيات متميزة تساعدك علي ان تخلصك من الحشرات لان تجد كل ذلك الا من خلال ركن البيت التي لديها كل الامكانيات في البحث عن شركة رش مبيدات بالدمام التي تقوم بالرش بواسطة فني مختص معه الادوات اللازمة للقيام بذلك وعطائك الضمانات اللازمة علي ذلك واليك خدمة نقل اثاث منزلية تساعدك في الوصول الي مكانك الجديد بواسطة شركة نقل اثاث بالدمام التي لديها كل الامكانيات والسيارات التي تساعدك علي تحقيق ذلك

  4. Replies

    1. نقدم لكم عملئنا الكرام افضل شركه تنظيف شركة تنظيف بابها شركة الشرق الاوسط لاعمال التنظيف ان عملية التنظيف تتطلب الجهد الكثير شركة تنظيف منازل بابها مع شركة الشرق الاوسط توفر لكل كل وسائل الراحه شركة تنظيف بخميس مشيط وايضا اسعار في منتهى الراحه كما ايضا تستخدم افضل انواع الالات والمعدات والعمالة المدربة فى اعمال التنظيف شركة تنظيف منازل بخميس مشيط مع شركة الشرق الاوسط نعم لبيت ومكتب وفيلا وشقه وغيرها نظيف تمام من الاوساخ شركة الشرق افضل شركة على الاطلاق فى اعمال التنظيف شركة تنظيف بنجران بجميع انواعه لا تقلق مع شركة الشرق الاوسط شركة نقل عقش ببيشة دائما فى راحة ان شاء الله خدمه 24 ساعه شركة تنظيف بحائل دون توقف

  5. نقدم لكم عمئلنا الكرام افضل شركة تنظيف فى بالطائف شركة تنظيف بالطائف شركة اوبى كلين ان عمليه التنظيف من اهم العوامل والوسائل التى تتطلب عملية شديد من الحرص والجهد والتعب اثناء عملية التنظيف شركة تنظيف منازل بالطائف مع شركة اوبى كلين وداعا تمام للتعب والجهد والمشقهه لان الشركة توفر لك كل انواع والعموال التى تساعدك فى عمليه التنظيف كما ايضا شركة اوبى كلين تمتلك افضل العماله المدربة على اكمل وجهه شركة تنظيف خزانات بالطائف حتى نوفر لك خدمه غير متاحه ال لدى شركة اوبى كلين وداعا للتعب بعد الان مع شركة اوبى كلين توفر لك كل سبل الراحه شركة مكافحة حشرات بالطائف لان الشركه متخصصه فى عملية التنظيف منذ زمن بعيد

  6. Automated Forex Trading : tradeatf Is An Automated Forex Investing Software. It Is An Algorithmic Trading Software That Provides Automated Forex Trading Signals.

  7. Trade FX At Home On Your PC: tradeatf Is A Forex Trading Company. The Company States That You Can Make On Average 80 – 300 Pips Per Trade. tradeatf States That It Is Simple And Easy To Get Started.