How to sleep 10 seconds before running a linux command? - linux

Simple question: I want to run a cron operation every minute at the 10th second (for example at 2:00:10 PM). The cron operation runs a curl command to download a website page. How can I run a cron operation to do that?
Current crontab setting:
* * * * * curl http://www.google.com/

* * * * * sleep 10;curl http://www.google.com/

Related

Run yarn script into crontab

I made a script in TypeScript that download data from some api and store inside a mongo DB.
If i run yarn start from the app folder it works well.
I would like to put this command in a cron job that will be executed every 5 minutes.
I try it with some sintax in crontab but ti doesn't work.
I try to put the call in a run.sh script but it doesn't work too.
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts
*/5 * * * * cd /opt/app-folder && /usr/bin/yarn start > /home/username/app-name-out.txt
*/5 * * * * /home/username/run.sh > /home/username/app-name-out.txt
*/5 * * * * /home/username/.nvm/versions/node/v16.15.1/bin/ts-node /opt/app-folder/src/main.ts > /home/username/app-name-out.txt
*/5 * * * * cd /opt/app-folder/src/ && /home/username/.nvm/versions/node/v16.15.1/bin/ts-node main.ts > /home/username/app-name-out.txt
Can someone help me to execute the main.ts every 5 minutes?
Thanks
I get rid of this problem.
There was 2 problems, the first related to the output redirection.
I fixed by redirect stdout in a file and stderr in another one.
The second was related the the $PATH of crontab: it was /usr/bin:/bin.
To fix it I log into my user where script works and I print my $PATH with echo $PATH.
I copied the value and I set it before the crontab line in crontab file.
This is what it looks like:
# Set the same path of user username to have the correct path in script
PATH=/home/username/.nvm/versions/node/v16.15.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
# Execute oracle every 5 minutes
*/5 * * * * /bin/sh /home/username/run.sh >> /home/username/app-name-info.txt 2>> /home/username/app-name-error.txt
Now it works.

Crontab every 5 minutes, but not on 5,10,15, etc

Some of my sites need regular crontabs, I use this to start a cronjob every 5 minutes "*/5 * * * *".
The crontabs are small, light, but there are starting to be several sites that need them, and starting them all together, it starts not being a very good idea.
With this "*/5" the cron starts at 5, 10, 15 20, etc... is it possible to make it start at, for example 8,13,18,23, etc?
Vixie cron accepts steps in a range (thanks Keith Thompson), so you can do
3-58/5 * * * * my_command
With other versions of cron, this may not be supported and you'd just have to do
3,8,13,18,23,28,33,38,43,48,53,58 * * * * my_command
Another option is something like
*/5 * * * * sleep 3m ; my_command
This could be adapted to sleep for a random time, thus further spreading out the jobs. For instance, you could do
*/5 * * * * /bin/bash -c 'sleep $((RANDOM/(32767/180))) ; my_command'
or use SHELL = /bin/bash further up in your crontab to make the /bin/bash -c unnecessary, if you're okay with using bash to run all the other cron jobs following the SHELL = line. $RANDOM in bash expands to a random integer between 0 and 32767, so this gets you a delay of up to 180 seconds.

Error in cron: bad minute errors in crontab file, can't install

I am trying to run a crontab with the expression given below. But i am getting bad minute error.
This is for a Linux Server.
0/5 * * * * /home/cdh_infa_user/data/pladmin/MyLinuxAgent/apps/Data_Integration_Server/data/scripts/Secureagent.sh
Do i need to install crontab? Please guide
enter image description here
enter image description here
To run cron every 5 minutes you need to add command like this:
*/5 * * * * /home/cdh_infa_user/data/pladmin/MyLinuxAgent/apps/Data_Integration_Server/data/scripts/Secureagent.sh
To run cron at 5 a clock you need record like this:
0 5 * * * /home/cdh_infa_user/data/pladmin/MyLinuxAgent/apps/Data_Integration_Server/data/scripts/Secureagent.sh

crontab doesn't run the links2 command

I'm trying run the following command every 1 minute using crontab, but the command doesn't run.
1 * * * * /usr/bin/links2 http://localhost/tomada/temperatura/servidor.php
The command works fine using the shell
What is the problem?
First -- don't use links for that purpose; it's built to be an interactive browser, but a cron job by its nature is noninteractive. curl is the right tool for the job:
* * * * * curl http://localhost/tomada/temperatura/servidor.php
...or, if you can't use curl (why?), then wget:
* * * * * wget -O - http://localhost/tomada/temperatura/servidor.php
Second -- if you must use links, use it in an explicitly noninteractive way, such as with -dump or -source:
* * * * * /usr/bin/links2 -dump http://localhost/tomada/temperatura/servidor.php

Can't make crontab work

I am new to Linux and Ubuntu and I seldom have to use it. I am trying to make this PHP script to run every minute using cron, but firstly I wanted to make some tests.
I created an empty file at /var/www/html/ called test. I ran on terminal:
sudo crontab -e
And added this line:
0 * * * * rm /var/www/html/test
Then saved it and exited. It said "Installing new Crontab"
Nothing happened. Then I created a file bfile.sh that contained:
#!/bin/sh
rm /var/www/html/test
and added the following to crontab:
0 * * * * bash /var/www/html/bfile.sh
Still nothing happened.
What do I have to do to see anything happening from crontab? By the way I checked and the service is running
0 * * * * basically says "run this at 0th minute of every hour."
If you need cron to run your command every minute do * * * * *.
0 * * * * runs once every 1 hour. If you want to run every minute it should be */1 * * * *
You can also check the /var/log/cron file for any errors

Resources