Complete newbie with AWS and relative newbie with bash. I want to set up a quick pull-and-append call every 10 minutes to a small text file on the NYC site that contains some real-time traffic data that I want.
The command curl --silent http://[IP.stuff.nyc_traffic_data].txt >> data.txt does exactly what I want it to. So, I created cron.txt with the contents
*/10 * * * * curl --silent http://[IP.stuff.nyc_traffic_data].txt >> data.txt
crontab cron.txt doesn't throw any obvious errors, but I don't seem to be able to get data.txt to update.
What am I missing? (Have done the usual research to find an answer, but I think the problem here is that I don't know what I don't know, as it were.)
Thanks!
Related
I'm testing Pushgateway along Prometheus, pushing simple data about my VMs' services every minute with a bash script and curl, triggered by crontab.
I have two bash scripts.
The first one, cpu_usage.sh, parse and send data about ps -aux command to pushgateway, via curl :
#!/usr/bin/env bash
z=$(ps aux)
while read -r z
do
var=$var$(awk '{print "cpu_usage{process=\""$11"\", pid=\""$2"\", cpu=\""$3"\"}", $3z}');
done <<< "$z"
curl -X POST -H "Content-Type: text/plain" --data "$var
" http://localhost:9091/metrics/job/top/instance/machine
Everything's ok on this one, my crontab send every minute the formatted data to localhost:9091, my pushgateway instance.
Now, I'm trying to send a parsed result of the service --status-all command, with a script called services_list.sh, exactly the way I've dealed with the cpu_usage.sh script :
#!/usr/bin/env bash
y=$(service --status-all)
while read -r y
do
varx=$varx$(awk '{print "services_list{service=\""$y"\"}", 1}');
done <<< "$y"
curl -X POST -H "Content-Type: text/plain" --data "$varx
" http://localhost:9091/metrics/job/top/instance/machine
When executing both scripts manually, like ./cpu_usage.sh and ./services_list.sh, everything's fine, Pushgateway is successfully retrieving data from both scripts.
But when I'm passing theses calls by CRON, only cpu_usage.sh is sending data to pushgateway (timestamp of last push on services_list method on Pushgateway stays unchanged).
My crontab syntax is like : * * * * * cd /path/ && ./script.sh
Scripts are in 777 / root:root, crontab is always edited as root. I've tried to concatenate both scipts in one bash file, and no matter the order I put them, the curl call for services_list method is never made (but everything's ok on cpu_usage method).
I'm a bit lost, as the two scripts are very similar, and manual calls on services_list.sh are working fine. Any thoughts ? Thank you.
Running on Debian 9 with Pushgateway 0.10.
Fixed :
The command service is not callable from crontab. I have to furnish the full path of the service ressource when calling it from cron, like /usr/sbin/service --options.
In this context, everything's running fine with y=$(/usr/sbin/service --status-all) in place of y=$(service --status-all) in my services_list.sh script.
I made a cron job in (Ubuntu 14 Trusty), like this way:
sudo crontab-e
then
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
* * * * * curl --silent http://www....cron_script.php > /dev/null
But sometimes, I get this error message to the crown job:
/etc/cron.hourly/curl:
/etc/cron.hourly/curl: line 5: bin: command not found
run-parts: /etc/cron.hourly/curl exited with return code 127
Can anybody tell me why? As you can see, it runs every minute, but once a hour I get that error message. Mostly on every hour at XX:54 minutes.
Thanks in advance!
Your crontab should'n contain the line:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Since you've put the path in the crontab hourly script it executes every hour which make you error show up
Create a script instead and put both lines in there
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
curl --silent http://www....cron_script.php
then in crontab just run the script
* * * * * my_script.sh > /dev/null 2>&1
Crontab should only contains lines which follows the format:
m h dom mon dow command
I think it is not able to find command curl while running from crontab, so you need to do which curl it will give you location of it in your box. Then try to give exact path of that curl command eg--> /usr/bin/curl into cron entry and let me know if this helps you. Also your path details should be added to the user's DOT/BASH profile by which you are running the crontab, then it could work without giving complete path of curl command too.
I'm looking for ways to have an automated process that checks the size of a flat file in a particular location if it is zero or otherwise. Currently, I am doing it manually by going to the location and to check it. This is tedious work since I'm doing this from time to time. Could someone guide me from here? It could be a shell script in a server that when I run, it will send an email to myself and others.
Hoping for your kind response. All I see here are batch files which is not applicable to me.
sample:
location
server_name/folder1/folder2/folder3
file: test_file.dat 0 bytes
You could use something like this:
#!/bin/bash
# Test file with full path
if [ ! -s /tmp/test.dat ]; then
mail 'alert#example.com' -s 'test.dat is empty' < /dev/null
fi;
Create a cron job using crontab -e that will check every five minutes
*/5 * * * * /home/user/check.sh
Be sure to set the execute bit on /home/user/check.sh with
chmod +x /home/user/check.sh
Notes:
The email content is the subject, there is no other text in the message because there is enough space to communicate the information on the subject line
You may need to use a program other than mail, depending on your OS, and you might have to update the command line parameters as well
I want to use Dropbox to keep track of my computer's IP address.
I was thinking of using crond to run a shell script every 5 minutes that asks icanhazip.com what my ip address is and then edit a file to have the current ip of that computer.
So i think the script would go something like this
CurrentIP = curl -s http://icanhazip.com/
echo $CurrentIP > ~/Dropbox/ComputerIP
then I would add an entry to crontab that would tell it to run every ten minutes
*/5 * * * * UpdateIP.sh
Mind you, this is my first shell script, so I have no idea what I´m doing, and would appreciate any input into how to do this more elegantly
The simplest would be to run your command directly from cron:
*/5 * * * * /usr/bin/curl -s http://icanhazip.com/ > ~/Dropbox/ComputerIP
(include the correct full-path to your curl command).
Of course, if you expect that you'd need to perform a more complicated task in the future, it is better to leave it in a script, as you already do.
I can't seem to run a simple bash file as a cron job that runs once a minute.
#!/bin/bash
NET_INTERFACE=eth0
CURRENT_IP=`/sbin/ifconfig $NET_INTERFACE | sed -n "/inet addr:.*255.255.25[0-5].[0-9]/{s/.*inet addr://; s/ .*//; p}"`
wget -q --delete-after "http://abc.co.uk/raspiUpdate.php?pi=1&ip=${CURRENT_IP}"
In the crontab I have:
* * * * * /home/user/Scripts/script.sh
Where am I going wrong?
Thanks
To troubleshoot this cron issue a few steps come to mind:
double-check the scripts file name you put in your crontab:
$ ls -lah /home/user/Scripts/script.sh
$ cat /home/user/Scripts/script.sh
check that that script is executable by the user the crontab is installed for. i.e. if you use /etc/crontab, it should be executable by root, if you used
$ crontab -e
it should be executable by your userid.
Actually running that script should already give you a hint.
look for hints provided by cron. Depending on what distribution you run, the file cron logs to is different. For debian it should be /var/log/syslog IIRC
If you can't find an error message anywhere, it's hard to tell what's wrong. Generally if I reach that point, I stop and go to sleep or do something else.
[edit]
And when I return, i often find the error. :)
p.s.:
i recently had problems with dots in the filename. Some cron implementations ignore files in /etc/cron.* with dots in their filename. Documented here: http://manpages.ubuntu.com/manpages/xenial/en/man8/cron.8.html as Debian specific. But I think I had this issue also on Fedora.