LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scheduled Command Line (https://www.linuxquestions.org/questions/linux-newbie-8/scheduled-command-line-4175659494/)

15101Redgate 08-20-2019 06:22 PM

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!

Mechanikx 08-20-2019 07:28 PM

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/

berndbausch 08-20-2019 08:03 PM

Quote:

Originally Posted by 15101Redgate (Post 6027531)
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.

scasey 08-20-2019 08:22 PM

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.

frankbell 08-20-2019 08:24 PM

berndbausch's suggestion is what I use to change my wallpapers every unit time.

chrism01 08-20-2019 11:14 PM

For cron you may find this worth a look https://www.adminschoice.com/crontab-quick-reference

Sefyir 08-21-2019 12:39 AM

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

Turbocapitalist 08-21-2019 01:38 AM

Quote:

Originally Posted by scasey (Post 6027558)
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.

aragorn2101 08-21-2019 04:08 AM

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.

15101Redgate 08-22-2019 04:53 PM

thanks to all, its working. please close thread

Mechanikx 08-22-2019 05:14 PM

Quote:

Originally Posted by 15101Redgate (Post 6028387)
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.


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