How to run a script at a certain time on Linux? [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I have a text file containing a specific date and time.
I want to be able to run a script at the time specified in that file.
How would you achieve that?
Create another script that runs in background (sort of a deamon) and checks every second if the current time is matching the time in the file?
Is there another way?
The machine is a linux server , Debian wheezy.
Thanks in advance

Look at the following:
echo "ls -l" | at 07:00
This code line executes "ls -l" at a specific time. This is an example of executing something (a command in my example) at a specific time. "at" is the command you were really looking for. You can read the specifications here:
http://manpages.ubuntu.com/manpages/precise/en/man1/at.1posix.html
http://manpages.ubuntu.com/manpages/xenial/man1/at.1posix.html

The at command exists specifically for this purpose (unlike cron which is intended for scheduling recurring tasks).
at $(cat file) </path/to/script

Cron is good for something that will run periodically, like every Saturday at 4am. There's also anacron, which works around power shutdowns, sleeps, and whatnot. As well as at.
But for a one-off solution, that doesn't require root or anything, you can just use date to compute the seconds-since-epoch of the target time as well as the present time, then use expr to find the difference, and sleep that many seconds.

Usually in Linux you use crontab for this kind of scduled tasks. But you have to specify the time when you "setup the timer" - so if you want it to be configurable in the file itself, you will have to create some mechanism to do that.
But in general, you would use for example:
30 1 * * 5 /path/to/script/script.sh
Would execute the script every Friday at 1:30 (AM)
Here:
30 is minutes
1 is hour
next 2 *'s are day of month and month (in that order) and 5 is weekday

Related

How to run a cron job every 5 hours (on Linux) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed last year.
Improve this question
I would like to run a cronjob every 5 hours.
Now it's 11:54pm..
Checking the crontab guru, this 0 */5 * * * seems to be correct, but the site also mentions something like this
next at 2022-01-07 00:00:00
I would like to know why? Does it mean it runs the script no matter what at midnight(00:00:00) ? Doesn't make sense.
The */5 syntax means "every 5 units, starting from 0". So, if you use it in the hour position, it will match hours 0, 5, 10, 15, 20.
I'm assuming what you actually want is a strict 5 hour interval. So after hour 20, you want the next run to be at 20 + 5 hours, so at 1AM not midnight. If that's correct, there isn't an easy way to make cron work like that. It can do even intervals of all divisors of 24 though: every 2 hours, every 3 hours, every 4 hours, every 6 hours, every 12 hours.
To get the 5 hour interval, one possible workaround is to
schedule the cron job to run every hour
at the start of your script that the cron job runs, add extra logic to check if it should run this hour. For example, you could take the current time, and calculate the hours since epoch. And exit early unless the calculated number is divisible by 5.

how to make a shell script run all the time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I am using ubuntu and I have written a python program to clean my download folder and a shell script to execute the program. But I need to run the script manually each time. Is there any way to run the script run automatically when a new file is added to that folder? I know there is inotifywait to monitor the folder but how to make that script runs from the time of reboot with setting corn??
I don't quite understand why you run Python script, from a bash script, and that Python script does clean your downloads folder. I think it would be much easier if you run just a bash script that cleans the downloads directly instead. This script would look like this:
#!/bin/bash
/usr/bin/rm -rf /home/<your_username>/Downloads
And save it as somescript.sh. Just remember to replace <your_username> with proper value.
As to how schedule running a script, one way to do this is using crontab utility in linux. It allows you to run programs at a certain time. Some examples you may find are here: crontab question, here crontab tutorial and here about crontab. Say, for example we would like to run above script at certain hour. Firstly run:
$ sudo crontab -e
To open crontab editor. Then you need to insert the line below to schedule running somescript at 6 pm everyday:
0 18 * * * /<path_to_the_script>/somescript.sh >/dev/null 2>&1
The entry in crontab has the following form:
minutes hour day month weekday (path to the script)
If you need help to start with specifying time, you might want to look at crontab generator. Also, just make sure that the script has the right privileges i.e. can be executed etc. After you save the crontab file it will be automatically scheduled. In case you want to schedule a task to run once, you might want to look into at commandline tool about at command.
Please note that you should more carefully prepare your question to make sure that everyone understands you. Ideally, provide some examples. This way you have a better chances to receive an answer that would actually help you, so I think it's a good deal. Welcome to the community :)
In order for a program to run always on a UNIX/Linux system, you might add it to the crontab, using the five asterisks, as mentioned in this serverfault post.
The best way to proceed here is to add a rm command in the crontab, using 2</dev/null at the end, in order for error messages (in case the file does not exist), not to flood your logs.

Take a screenshot in bash at a specific time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
So,I need to compile a code which is going to take a bunch of time to compile, at least 2-3 hours. I wanted to find a way in the bash command line, that will take a screenshoot of my screen at a specific schedule.Now,I'm going to fall asleep, and my pc has 4 hours of battery left and I'm bored to move from my bed to find my adapter which is downstairs and plug in my pc.
And If I let my pc running, it is going to be out of power before i wake up.
I've tried in my ubuntu: echo"scrot"| at thehour,
But idk why,it doesnt work.
You can use this command:
gnome-screenshot -d 5
Or:
gnome-screenshot -d 5 -f ~/Desktop/Screenshots/filename.png
The -d 5 means that the will take the screenshot in 5 seconds.
The difference between both commands is that the first will ask for you (with a GUI) to save the image in the path you want. The other it will not be necessary ask for you the path. It's saved automatically after 5 seconds.
Or you can also use the command import -window root $HOME/Desktop/filename.png
That will take a screenshot and will be saved in that path.
To can use the import command you have to install the package image-magick:
sudo apt-get install imagemagick
After you have it installed, you can schedule the time with a bash script that executes the command import -window root $HOME/Desktop/filename.png. You can use sleep to can do that.
Or you can schedule the time with KShutdown, you will have to install it too.
sudo apt install kshutdown
Then, when you have installed the program, open it and will show something like this:
You have to select the Extras option and then, if you don't have scripts in the path (in my case is: ~/.local/share/kshutdown/extras), the program will ask for you create the script.
So in that path, you will have to create a script with this content:
#!/bin/bash
import -window root $HOME/Desktop/filename.png
or
gnome-screenshot -f ~/Desktop/Screenshots/filename.png
Now, if all is correct, the program will detect that script, and you can select it.
And to finish you will have to select the time you want the script runs.
You can select a certain time, like "23:30" or you can select Time From Now to run the script in certain minutes, seconds, hours...
Finally you press ok and the program will do what you want!

Two linux users with different clocks [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This question does not appear to be about programming within the scope defined in the help center.
Improve this question
Is it possible to have two linux users with slight delays on their clocks?
The reason I ask is I have two scripts executed by the cron every minute (one on each user). One script copies a file from the another machine the other loads the data in the file into mysql.
We have been finding the loading of the data misses the first minute 90% of the time. I think this is because it is called exactly the same time as the call to copy the file from the other machine is executed.
If I could delay the user clock whose cron executes the loading script by 5 seconds i think this would solve the problem.
Perhaps there is another way of achieving this? Something easier I am missing. I would like the first script to be called every round minute and the second to be called 5seconds after every round minute.
Add a sleep 5 or sleep 10 to the start of the cron entry.
Probably the best answer to this is to set up xntp so the clocks are in sync. Then, as glglgl suggests, put a sleep at the top of the script you want delayed.

Need to determine when linux/OSX is fully initialized [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This question does not appear to be about programming within the scope defined in the help center.
Improve this question
I need a shell scripting way to determine when a remote linux/OSX has completed all /etc/init.d/* for what ever run levels I have chosen to run in.
since I know I can ssh into a virutal box on my net while it is still booting and run commands like /bin/true or /bin/ps that not everthing is in the state it needs to be.
essentially I need to
while [ ${INIT_STILL_RUNNING_ON_REMOTE_SYSTEN} ]
do
sleep 30
done
ssh remoteUser#remoteSystem:command
You could put a script in the run-level you are interested in that is the last thing to run and first thing to shut down that touches a file somewhere to indicate it has run.
For instance, if you made a script called S99finished and put it in the run-level folder it would run last at that run-level. A corresponding K00finished would run first when shutting down.
S99finished could look something like:
#!/bin/bash
touch ~/.init_finished
and K00finished could look something like:
#!/bin/bash
if [ -e ~/.init_finished ]; then
rm ~/.init_finished
fi
Then your startup script would poll until ~/.init_finished existed at which point it would go on it's merry way.
Note, the startup scripts run as root, so using the home directory tilde will put it in root's home. That's probably not ideal for what you're doing, but illustrates the point. It's just as easy to put it in /var/log or somewhere else common to poll from. Just remember it has to have read permissions for everybody wherever you stick it.

Resources