Pass variable through shell script via crontab? - sh

Is there a way to pass a variable through a cron job? Example, currently I have a cron job setup like so:
* * * * * /bin/bash /Volumes/raid/farm_script/_apps/_scripts/farm_ping.sh
I'm wondering, is it possible to pass through info to the shell script from the cron job? Something similar to this perhaps:
* * * * * /bin/bash /Volumes/raid/farm_script/_apps/_scripts/farm_ping.sh 192.168.2.17
If so, how could I access this inside the farm_ping.sh file?

This is a positional parameter, accessible as $1. The next one would be $2.

Related

write command in interactive prompt (powershell or bat)

I work in program with interactive prompt call XDIAL
I want write *select * from pcastd* in this prompt like this
D:\Aither\Exe\xdial.exe
start-sleep-seconds 1
/C "select * from pcastd"
xdial interactive prompt
there is not "select * from pcastd" writed ...
how to do this ?
As an alternative to interactive input, command-line utilities typically accept input via the pipeline, which sends an input command's output to the target utility's stdin (standard input):
Therefore, you can echo the string of interest and pipe it to xdial.exe:
# From PowerShell
'select * from pcastd' | xdial.exe
REM # From cmd.exe
REM # Note that there's no space before the "|" by design,
REM # because a space would become part of `echo`'s output.
echo select * from pcastd| xdial.exe

Crontab command not outputting any log

I've been reading this great post:
https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it
And I decided to modify a line of my cron task to output my echos and any problems it might encouter. My cron tab line looks like this:
30 08 * * * /root/scripts_server/backup_daily.sh &>/var/log/bkp_daily.log
The script runs correctly (I can confirm that the backups were made and transfered) and the output file is created (bkp_daily.log), but it is empty.
Can any one point out a problem?
EDIT:
This is an example of a line in the script:
echo "--------------Sincronización de git remotos a locales-----------------------"
I think &> is a bash extension, try using the standard shell syntax:
30 08 * * * /root/scripts_server/backup_daily.sh >/var/log/bkp_daily.log 2>&1

overwrite then append output of a cron job each time it runs

I know you can redirect the output of a cronjob via ">" to overwrite and ">>" to append. However, I was wondering if there is anyway to get the output from a cronjob to overwrite the log file each time the job is run, but then append the output for that particular job run?
When you use > it overwrites anything previously each time there is a in the output of the command linebreak, so you don't see historical output from that particular job.
If I understand it correctly, you want to create a new log file everytime the job is run, so in crontab you use ">" as
* * * * /home/myhome/some_cron_job.sh > /home/myhome/cron_job_output
Now, within some_cron_job.sh, you use ">>" to append to the log file
(within shell script)
echo "Testing" >> /home/myhome/cron_job_output
Does that help ?

file path for a Cron job script in a zendframework project

I have a zendframework project. I need to create a cronjob from the Cpanel. But I have one doubt. Where I put the cronjob script file (in public_html? or anywhere in the project as in the usual manner). Then what is the path i have to give in the Command textbox in Cpanel
Please reply anyone. I am still waiting .... Please ...
Thanks
You can really put it wherever you like.
I typically create a directory in my project root called "cli/", and put stuff in there.
Usually I'll have a little boot.php script that looks a lot like my app's index.php, but ends with $application->bootstrap(); instead of $application->bootstrap()->run();
As for your cron job, you can do something like:
* * * * * ENVIRONMENT=production /usr/bin/php /path/to/your/project/cli/script.php

How to test crontab entry?

I have an entry in my crontab that looks like this:
0 3 * * * pg_dump mydb | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz
That script works perfectly when I execute it from the shell, but it doesn't seem to be running every night. I'm assuming there's something wrong with the permissions, maybe crontab is running under a different user or something. How can I debug this? I'm a shared hosting environment (WebFaction).
You need to escape "%" characters in crontab entries with backslashes- see the crontab(5) manpage. I've had exactly the same problem.
For example:
0 7 * * * mysqldump usblog | bzip2 -c > usblog.$(date --utc +\%Y-\%m-\%dT\%H-\%M-\%SZ).sql.bz2
Do you not get emails of cron errors? Not even if you put "MAILTO=you#example.com" in the crontab?
You may also need to set PATH in your crontab if pg_dump or gzip isn't on the system default path (so use "type pg_dump" to check where they are, crontab usually only runs commands in /bin or /usr/bin by default)
Always use full paths in crontab entrties. For example, /usr/bin/gzip. You will also need to do that for pg_dump and date.
When you say it doesn't work, what do you mean? Does it not generate the file at all or is it empty?
If your system is set up correctly, crontab should send you an email if your command generated any output.
Try something like this to verify crontab is running. It will touch the file every minute.
* * * * * touch /tmp/foo
And check your paths like James mentioned.
If this is in something like /etc/crontab, make sure the user is included:
0 3 * * * <user_goes_here> pg_dump mydb | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz