Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to "lock down" the computer by running a script at boot that turns lock on, lock screen after 10 min and require password, etc. I am using Ubuntu 12.04

I have tried editing crontab, anacrontab as well as rc.local to either run my script or actually inserted the code from the script into the files and I cannot get anything to work.

Any ideas?

* this is the only way I can get the commands to run in a script manually *

(su - $(logname) -c "gsettings set org.gnome.desktop.screensaver lock-enabled true")

(su - $(logname) -c "gsettings set org.gnome.desktop.screensaver lock-delay 600")

(su - $(logname) -c "gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend true")

(su - $(logname) -c "gsettings set org.gnome.desktop.screensaver user-switch-enabled false")

* These commands work if entered into terminal manually *

gsettings set org.gnome.desktop.screensaver lock-enabled true
gsettings set org.gnome.desktop.screensaver lock-delay 600
gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend true
gsettings set org.gnome.desktop.screensaver user-switch-enabled false

I also tried inserting the following code from other posts about using gsettings in cron with no luck:

sessionfile=`find "${HOME}/.dbus/session-bus/" -type f`
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'`

Also I double checked my script was executable and created /home/username/.config/autostart/screenlock.desktop:

[Desktop Entry]
Name[en_US]=screen lock
Name=screen lock

Also adding /path/screenlock.sh to bash.rc or .profile do not seem to work as well.

share|improve this question

1 Answer 1

Instead of using:

sessionfile=`find "${HOME}/.dbus/session-bus/" -type f`
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'`

try to use:

PID=$(pgrep gnome-session)  # instead of 'gnome-session' it can be also used 'noutilus' or 'compiz' or the name of a process of a graphical program about that you are sure that is running after you log in the X session
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

See the explanation in my answer here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.