8

I would like to make a sudo command (sudo service smbd restart) run after 1 minute of being logged on. How would I go about doing this?

P.S. This is a system with no monitor, mouse, keyboard or speakers connected - it's a printer and file server.

landroni
  • 5,941
  • 7
  • 36
  • 58
user2235532
  • 89
  • 1
  • 2
  • 1
    What do you mean by logged on? 1 minute after start computer or 1 minute after a user have logged in? – jhilmer Feb 10 '14 at 22:05
  • possible duplicate http://askubuntu.com/questions/814/how-to-run-scripts-on-start-up – Lynob Feb 10 '14 at 22:09
  • What Fischer said, also see http://stackoverflow.com/questions/3964254/bash-script-to-run-in-5-minutes – Richard Feb 10 '14 at 23:29
  • 1
    If some answer satisfy the OP, please mark it as answered. See http://meta.askubuntu.com/questions/8333/unanswered-questions-what-to-do – Rmano Feb 13 '14 at 16:34

2 Answers2

10

A) If it's at system start-up, add this to the end of your /etc/rc.local (1): (before the exit 0, obviously):

( sleep 60 && service smbd restart )& 

Note:

  1. the outer () are needed so that the complex command detach itself and go to the background, allowing the boot process to finish;
  2. sudo is not needed there, /etc/rc.local is executed by root;
  3. Are you really sure this is a solution? It is a race condition asking to happen...

B) if it's at user login, you need two steps:

  1. configure your sudo so that it will not ask for a password for service smbd restart command (see How do I run specific sudo commands without a password?);

  2. prepare a script with the following contents and add it to your autorun/startup program (varies with the desktop environment you are using).

Script:

#!/bin/bash
( sleep 60 && service smbd restart )& 

Footnotes

(1) check if /etc/rc.local is executable. Otherwise, make it so with sudo chmod +x /etc/rc.local

Rmano
  • 31,947
4

Try man sleep:

sleep 60 && sudo service smbd restart

Put this in the autorun programs or scripts executed at login time.

landroni
  • 5,941
  • 7
  • 36
  • 58
  • If you do not configure appropriately the sudoers file, it will not work (will stop asking for a password). See http://askubuntu.com/questions/159007/how-do-i-run-specific-sudo-commands-without-a-password – Rmano Feb 13 '14 at 16:24
  • 1
    Agreed. Your answer is certainly more complete than mine. Here I was simply trying to point the user towards a solution. – landroni Feb 13 '14 at 16:26