LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-07-2013, 09:22 AM   #1
QueenZ
Member
 
Registered: Sep 2008
Distribution: openSUSE, Ubuntu
Posts: 372
Blog Entries: 2

Rep: Reputation: 32
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!
 
Old 11-07-2013, 09:26 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
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
 
Old 11-07-2013, 09:28 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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
 
1 members found this post helpful.
Old 11-07-2013, 09:29 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,023

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Command at(1) is your friend:

Code:
$ echo './myscript.sh' | at 18:45
job 2 at Thu Nov  7 18:45:00 2013
 
Old 11-07-2013, 09:37 AM   #5
QueenZ
Member
 
Registered: Sep 2008
Distribution: openSUSE, Ubuntu
Posts: 372
Blog Entries: 2

Original Poster
Rep: Reputation: 32
Quote:
martin@martin-ubuntu:~$ at 17:37 < echo "Hello"
bash: echo: No such file or directory
why I get this?
 
Old 11-07-2013, 09:45 AM   #6
QueenZ
Member
 
Registered: Sep 2008
Distribution: openSUSE, Ubuntu
Posts: 372
Blog Entries: 2

Original Poster
Rep: Reputation: 32
ok I made this script but nothing happens...

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

lunchchrome.sh contains
#!/bin/sh
exec google-chrome
 
Old 11-07-2013, 10:06 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by QueenZ View Post
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
 
Old 11-07-2013, 10:10 AM   #8
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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.

Last edited by btmiller; 11-07-2013 at 10:12 AM.
 
Old 11-07-2013, 11:33 AM   #9
QueenZ
Member
 
Registered: Sep 2008
Distribution: openSUSE, Ubuntu
Posts: 372
Blog Entries: 2

Original Poster
Rep: Reputation: 32
Oh that worked! Thank you so much!
 
Old 11-07-2013, 12:48 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a shell script (with source command)???? kebabbert Programming 9 11-21-2010 11:06 AM
how to execute Source command in a shell script UltraSoul Linux - General 3 09-23-2010 10:13 AM
[shell script] execute command and parse output stoiss Programming 2 01-26-2009 01:49 AM
Not able to execute Oracle Sql command in shell script Ranvijaidba Programming 3 10-23-2008 03:45 PM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 09:16 AM


All times are GMT -5. The time now is 10:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration