LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Execute command on time Shell Script (https://www.linuxquestions.org/questions/linux-newbie-8/execute-command-on-time-shell-script-4175483742/)

QueenZ 11-07-2013 09:22 AM

Execute command on time Shell Script
 
Hello, I want to say I am a complete newbie but I want a shell script that will execute a command on a specific time for example on 6:45 am

How is this done, a full example would be very useful! Thank you!

TobiSGD 11-07-2013 09:26 AM

On Linux systems a service called cron is launching whatever you want at any time you want. It is pre-installed on all Linux systems, no need to write a script for that.
http://unixgeeks.org/security/newbie/unix/cron-1.html

Alternatively you can use the at command, for more info look at
Code:

man at

druuna 11-07-2013 09:28 AM

Assuming you want to do this once: Use the at command.

Something like this:
Code:

at 06:45 < name_of_script
Have a look here for more details: Linux and Unix at, batch, atq, atrm command

NevemTeve 11-07-2013 09:29 AM

Command at(1) is your friend:

Code:

$ echo './myscript.sh' | at 18:45
job 2 at Thu Nov  7 18:45:00 2013


QueenZ 11-07-2013 09:37 AM

Quote:

martin@martin-ubuntu:~$ at 17:37 < echo "Hello"
bash: echo: No such file or directory
why I get this?

QueenZ 11-07-2013 09:45 AM

ok I made this script but nothing happens...

at 17:43 < /home/martin/Desktop/lunchchrome.sh

lunchchrome.sh contains
#!/bin/sh
exec google-chrome

druuna 11-07-2013 10:06 AM

Quote:

Originally Posted by QueenZ (Post 5060087)
ok I made this script but nothing happens...

at 17:43 < /home/martin/Desktop/lunchchrome.sh

lunchchrome.sh contains
#!/bin/sh
exec google-chrome

This will not work because you are trying to start an X related command, which needs a display to connect to.

Try changing your script to the following:
Code:

#!/bin/sh
export DISPLAY=:0
/opt/google/chrome/google-chrome &

Make sure the italic part is correct. Run which google-chrome and copy paste the result.

Also make sure that your script is executable:
Code:

$ chmod 755 /home/martin/Desktop/lunchchrome.sh

btmiller 11-07-2013 10:10 AM

Try:

at -f /home/martin/Desktop/lunchchrome.sh 17:45

Also, you can use the atq command to make sure that the command has been queued properly.

Some systems don't have at installed any more my default (e.g. my Arch Linux machine). There are various ways to get cron to execute a command once at a particular time. If you want to repeatedly execute a command at a set interval, cron is the way to go.

edit:

As druuna mentioned, you also need to make sure that you have a proper DISPLAY to launch X based apps. Note that you'll only be able to launch apps as the user running the X server on the local machine, unless you use xhost to modify permissions.

QueenZ 11-07-2013 11:33 AM

Oh that worked! Thank you so much!

druuna 11-07-2013 12:48 PM

Nice to read you got this fixed and you're welcome!

BTW: Can you put up the [SOLVED] tag.
- above the first post -> Please Mark this thread as solved if you feel a solution has been provided.
- -or- -
- first post -> Thread Tools -> Mark this thread as solved


All times are GMT -5. The time now is 07:46 PM.