LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-20-2019, 07:22 PM   #1
15101Redgate
LQ Newbie
 
Registered: Aug 2019
Posts: 2

Rep: Reputation: Disabled
Scheduled Command Line


Hello

I have a command line I need to run every 15 minutes.

How do I do that? I have seen cron jobs call a script, but I don't know what a script is (fine and extension) and set a cron job to call it

Thanks!
 
Old 08-20-2019, 08:28 PM   #2
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 183

Rep: Reputation: 146Reputation: 146
A shell script is an executable file containing shell commands or even just a single command, often including programming contructs such as "if", "else", "while", etc.

For example, to create a shell script called "my_script.sh":

Code:
#!/bin/sh

echo 'I'm a shell script!'
Next make it executable:

Quote:
chmod u+x my_script.sh
then execute it:

Quote:
./my_script.sh
Quote:
I'm a shell script!
The first line is known as the "she-bang" line and it's used to determine the shell/interpreter to use. On most systems /bin/sh is a symlink to the bash shell. The ".sh" extension is optional.

As far as setting up a cron job, I don't have much experience with them, so maybe another member can help you with that. This link could be a good starting point.

https://www.cyberciti.biz/faq/how-do...-or-unix-oses/

Last edited by Mechanikx; 08-20-2019 at 08:29 PM.
 
Old 08-20-2019, 09:03 PM   #3
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 4,140

Rep: Reputation: 1162Reputation: 1162Reputation: 1162Reputation: 1162Reputation: 1162Reputation: 1162Reputation: 1162Reputation: 1162Reputation: 1162
Quote:
Originally Posted by 15101Redgate View Post
I have a command line I need to run every 15 minutes.

How do I do that? I have seen cron jobs call a script, but I don't know what a script is (fine and extension) and set a cron job to call it
Here is a very simple solution without cron. It assumes your shell is bash:
Code:
while true
do
   your command line
   sleep $((15*60))
done
You can put this code in a file and execute that file. Or just run it on the command line, whatever suits you best. You can add an ampersand (&) after the done in order to run the loop in the background.

Last edited by berndbausch; 08-20-2019 at 09:04 PM.
 
1 members found this post helpful.
Old 08-20-2019, 09:22 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,376

Rep: Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551Reputation: 1551
Please look at man cron and man crontab to start.
cron is easy enough once one "gets it," but getting there is a tad confusing.

Read the man pages. Come back here with specific questions about things you need clarified.

Maybe share the command you want to run, too. You may not need a script if the command is simple enough.
 
Old 08-20-2019, 09:24 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 15,890
Blog Entries: 27

Rep: Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669Reputation: 4669
berndbausch's suggestion is what I use to change my wallpapers every unit time.
 
Old 08-21-2019, 12:14 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,707

Rep: Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497Reputation: 2497
For cron you may find this worth a look https://www.adminschoice.com/crontab-quick-reference
 
Old 08-21-2019, 01:39 AM   #7
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 607

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
If you're running a simple command cron makes that pretty easy

Run simple_command every 15 minutes -> crontab -e

Code:
*/15 * * * * simple_command
Be aware cron has a limited environment. So to run ls I would want to run it like this

Code:
*/15 * * * * /bin/ls
 
1 members found this post helpful.
Old 08-21-2019, 02:38 AM   #8
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,457
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
Quote:
Originally Posted by scasey View Post
Please look at man cron and man crontab to start.
cron is easy enough once one "gets it," but getting there is a tad confusing.

Read the man pages. Come back here with specific questions about things you need clarified.

Maybe share the command you want to run, too. You may not need a script if the command is simple enough.
Just to add to the confusion, the program crontab(1) and the file format for crontab(5) are documented separatly. So to read the documentation on the file format, try man 5 crontab.
 
1 members found this post helpful.
Old 08-21-2019, 05:08 AM   #9
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 552

Rep: Reputation: 271Reputation: 271Reputation: 271
Hi,

If you had searched for "linux run command every 15 minutes" you would have been surprised how much help you can find on the web.

Anyway, I just stumbled upon a very interesting page: https://crontab.guru/
You can play with this to understand how cron works. You can also play with https://crontab-generator.org/.

Basically your cron command will look something like this:
*/15 ... PATH_TO_YOUR_SCRIPT

Then, you just run
Code:
crontab -e
This opens a vi editor where you press "I" to edit and type in your cron command. Then you press ESC followed by typing ":x" in order to save and exit. Everytime you run crontab -e command you have to press "I" to type and ESC+":x" to save and exit.

Now a bash script is simply a source file where every line is a command like the ones you type in your shell. Check out these:
https://www.tldp.org/LDP/Bash-Beginn...tml/index.html

All the best.
 
Old 08-22-2019, 05:53 PM   #10
15101Redgate
LQ Newbie
 
Registered: Aug 2019
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks to all, its working. please close thread
 
Old 08-22-2019, 06:14 PM   #11
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 183

Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by 15101Redgate View Post
thanks to all, its working. please close thread
You can mark this thread as solved by clicking on "thread tools" at the top right of your original post, then select "mark thread as solved" from the drop down menu. This can only be done by the OP.

Last edited by Mechanikx; 08-22-2019 at 06:17 PM.
 
  


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
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 09:04 AM
Print all PID folders from /proc line-by-line with this format (( PID: command-line )) courteous Linux - Newbie 7 12-12-2010 05:47 PM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 06:25 AM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 09:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:10 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