I'd like to force my account to log out at a specific time as a self-restraint measure (e.g., to ensure I go to bed on time). And for the script to run whenever I try logging in after that time between two specified times (e.g., 11pm-5am). Potentially tied to internet time so it cannot be avoided through simply changing the time. Perhaps making use of:

sudo pkill -KILL -u <username>
  • 1
    Is it important to keep the computer running; only logging out your user? It is a simpler option to shutdown the computer (gracefully) at a specified time. You can also consider a warning five minutes before logout/shutdown. – sudodus Jul 12 '18 at 8:51
  • 1
    A shutdown with a five minute warning would also be an ideal solution. So long as the user (being me) could not simply turn the computer back on and proceed to log in. Relatedly, there is a Windows/Mac app that continuously sends a lock screen command between certain hours you specify (Cold Turkey) and thus achieves my goal. But is unavailable on linux. – user838196 Jul 12 '18 at 9:07
  • 1
    Weird solution: 1) setup a cronjob for root from 11pm to 5am running every 10 minutes with pkill -KILL -u <username> 2) configure sudo so that the sudo command is not allowed between 11pm and 5am (see NOTBEFORE, NOTAFTER tags in the man page), so you cannot undo the cronjob in these hours. – PerlDuck Jul 12 '18 at 9:22
  • @PerlDuck, could you make an answer from this comment (maybe consider shutdown, not only logout according to the OP's comment). When done, please tell me, so that I can upvote it :-) – sudodus Jul 12 '18 at 10:11
  • 1
    Have a look at the script I wrote, Lock Screen Timer. I could modify it to your specifications: askubuntu.com/questions/837078/… – WinEunuuchs2Unix Jul 12 '18 at 11:30

The command sudo pkill -KILL -u <username> would be suitable to log another user out. To execute this commands to log you forcefully out at a specific time, you may set up a cron job that executes the command at 11 pm. cron is still installed by default on Ubuntu 18.04. Alternatively, on more recent linux systems including Ubuntu that use systemd, systemd can be used to run jobs on specific times.

Between 11pm-5am, you could block your possibility to login by locking your password. Credit goes here. Apparently it is possible to deny login access during certain times by adding an entry in /etc/security/time.conf. In your case, the line would look like: login ; * ; !<youruser> ; !Al2300-0500

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy