Script to log out automatically

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
bloup

Script to log out automatically

Post by bloup »

Hey everyone! I think my script will solve your problems. Basically, there is a very simple little program called xprintidle that just prints the idle time of the current user's X session to stdout.

Code: Select all

#!/bin/bash

MAXIDLE = 60000 #Maximum amount of idle time in milliseconds

while [ 1 ]; do
    if [ `xprintidle` -gt $MAXIDLE ]; then
        mate-session-save --force-logout #Whatever program you use to logout.
        break
    fi
    sleep 1
done
And then just make it a startup application.

You can even add a nested if loop, if you want like a warning message or something, like so:

Code: Select all

#!/bin/bash

MAXIDLE = 60000 #Maximum amount of idle time in milliseconds
DELAY = 30 #Amount of warning time you want to give

while [ 1 ]; do
    if [ `xprintidle` -gt $MAXIDLE ]; then
        notify-send "Foo" "Bar" -t `expr $DELAY '*' 1000` #Whatever warning message you want.
        sleep $DELAY
        if [ `xprintidle` -gt $MAXIDLE ]; then
            mate-session-save --force-logout #Whatever program you use to logout.
            break
        fi
    fi
    sleep 1
done
For the warning message, you can get really creative there. You could probably use Zenity if you wanted instead or maybe even you could embed the $DELAY variable in the warning message like "You will be logged out in $DELAY seconds".

Hope that helps you all out! This was one, of many, dead end threads I found, and I figure I can't be the only one who could really find this handy so hopefully I save other people some heartache.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Script to log out automatically

Post by xenopeek »

You replied this post to this topic: http://forums.linuxmint.com/viewtopic.p ... 2&start=20. The last comment there is over a year old and the topic is over three years old. Please don't reply to support requests older than 6 months, see the forum rules. I think you post would be more at home here :D
Image
Locked

Return to “Scripts & Bash”