Generated file in Crontab is empty - grep

I need help with this one. An Artisan command is being run by crontab to analyze a logfile and generate a report into an HTML format.
Here is the line of code in the command that is being executed by crontab:
$today = date("d/M/Y");
exec('grep "$today" /path_to_logfile | goaccess -a > /path_of_generated_HTML_file');
exit();
Unfortunately, the file being generated by cron is empty but when you execute the same code directly in the terminal, it successfully generates an html file. I appreciate your help guys.

This is most likely because your PATH is configured incorrectly when running under crontab. For most implementations of crontab, you can simply specify the PATH on the first line on the crontab file opened with crontab -e:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
You could also set the path inside PHP if you so desire:
<?php
putenv('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin');
$today = date("d/M/Y");
exec('grep "$today" /path_to_logfile | goaccess -a > /path_of_generated_HTML_file');
exit;

Related

Unix. Parse file with full paths to SHA256 checksums files. Run command in each path/file

I have a file file.txt with filenames ending with *.sha256, including the full paths of each file. This is a toy example:
file.txt:
/path/a/9b/x3.sha256
/path/7c/7j/y2.vcf.gz.sha256
/path/e/g/7z.sha256
Each line has a different path/file. The *.sha256 files have checksums.
I want to run the command "sha256sum -c" on each of these *.sha256 files and write the output to an output_file.txt. However, this command only accepts the name of the .sha256 file, not the name including its full path. I have tried the following:
while read in; do
sha256sum -c "$in" >> output_file.txt
done < file.txt
but I get:
"sha256sum: WARNING: 1 listed file could not be read"
which is due to the path included in the command.
Any suggestion is welcome
#!/bin/bash
while read in
do
thedir=$(dirname "$in")
thefile=$(basename "$in")
cd "$thedir"
sha256sum -c "$thefile" >>output_file.txt
done < file.txt
Modify your code to extract the directory and file parts of your in variable.

Working bin/bas returns error when executed from run script Xcode

I have a working shell script that works with no problems but when I executed it with a run script i get the following error:
The script will scan all the proto files in a directory and convert it to Swift using ProtoBuf. After that I will move the swift files into an App folder. The script code is the following
#!/bin/bash
for protoFile in ./*.proto;
do
protoc --swift_out=. $protoFile
done
for file in ./*.swift;
do
mv $file ../Convert\ AV/Model/USBDongle/Proto/
done
Any ideas?
Thank you
I was calling the script from the directory it was located. When Xcode executes the the script that assumption is false. So I am getting the directory where the script is located and I do the logic with that path
#!/bin/bash
#Get Directory where the script is located
baseDirectory=$(dirname "$0")
echo "$baseDirectory"
for protoFile in "$baseDirectory"/*.proto
do
echo $protoFile
protoc --swift_out="$baseDirectory" -I "$baseDirectory" "$protoFile"
done
for protoFileSwift in "$baseDirectory"/*.swift;
do
echo $protoFile
mv "$protoFileSwift" "$baseDirectory"/../Convert\ AV/Model/USBDongle/Proto
done
* /* Makes like is a comment... *

Cron job to keep rails server running

I'm trying to add the following line to the crontab for a rails app:
* * * * * lsof -i tcp | grep -v grep | grep -q puma || /home/me/app_name/bin/rails s
... in short, I have an error that occasionally takes down my rails server, and I would like to run this command every minute to make sure the server remains as available as possible. (The actual error is a question for another day.)
Initially, I got an error:
shared_helpers.rb: 34
in 'default_gemfile': could not locate Gemfile (Bundler::GemfileNotFound)
... which traced back to line 4 in bin/rails:
load File.expand_path('../spring', __FILE__)
I changed the ('../spring') to ('./spring'), because the spring script was located in the same directory as the rails script, not in the parent. (Oddly, running rails s from the command line works whether I use '..' or '.' in the path.)
This, at least got rid of the error above, but now I have the following:
/bin/sh: 1: lsof: not found
/bin/sh: 1: grep: not found
/bin/sh: 1: grep: not found
/usr/bin/env: 'ruby_executable_hooks': No such file or directory
I get these errors once per minute, so at least I know the time format my entry is correct.
The relevant part of my crontab looks like this:
PATH=$PATH:/usr/share/rvm/rubies/ruby-2.4.1/bin/
GEM_PATH=$GEM_PATH:/usr/share/rvm/gems/ruby-
2.4.1:/usr/share/rvm/gems/ruby-2.4.1#global
* * * * * lsof -i tcp | grep -v grep | grep -q puma || /home/me/app/bin/rails s
I also tried:
* * * * * lsof -i tcp | grep -v grep | grep -q puma || /usr/share/rvm/gems/ruby-2.4.1/bin/rails s
... but get the same error.
I can run rails s from the command line, as I mentioned. I know running the command from a logged-in shell has the benefit of login scripts being read and environment variables being set, but I don't know what I need to do to get the environment in this crontab to co-operate.
NOTE: I'm not using the whenever gem for this, because I'm using external commands like grep and lsof; if there is an equivalent rails-based solution (to test if the server is running, and start it if it isn't), I could do that instead of using the crontab. (But I don't know what that would be.)

Can I grep each file of directory and save output to one file? Example code included

I am running a grep command on each file in a directory. I want the outputs to be appended into the same file. Is this possible?
Here is what I am using unsuccessfully:
for f in /directory/*.txt
do
grep -Eo "[0-9]+\.[0-9]+" $f >> one_output_file.txt
done
I am grepping out a number from each file and I want the numbers to be listed together in ONE output file. Possible?
Thanks!
Why not drop the for loop and do
grep -Eoh "[0-9]+\.[0-9]+" /directory/*.txt > one_output_file.txt

whenever gem: I set :output but the logfile doesn't show up where I'd expect it to

In my schedule.rb file I have the following lines:
set :output, '/log/cron_log.log'
every 5.minutes do
command 'echo "hello"'
end
I ran whenever -w as suggested in this question Rails, using whenever gem in development, and I assume the cronfile is written and running. (I tried restarting the Rails server as well.)
And when I run $ crontab -l I see the following:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash
-l -c 'echo "hello" >> /log/cron_log.log 2>&1'
But I can't find the log file. I checked in rails_project/log, ~/log and /log to no avail. On OSX btw.
How can I get the log file to be written to the rails project log folder?
Where is your log?
You're putting the output file at the highest directory level:
$ cd /log
To see if the file exists and if it has data in it:
$ ls -la cron_log.log
To create the log file if needed:
$ touch cron_log.log
To open up permissions for your own local debugging (do NOT do this in production!)
$ chmod +rw cron_log.log
Is your command running?
To run the command manually to find out if it works as you expect:
$ /bin/bash -l -c 'echo "hello" >> /log/cron_log.log 2>&1'
To improve your security and protect your path, use full paths:
wrong: command 'echo "hello"'
right: command '/bin/echo "hello"'
To find the command full path:
$ which echo
To verify the cron is running as you expect:
$ sudo grep CRON /var/log/syslog
The grep result should have lines that something like this:
Jan 1 12:00:00 example.com CRON[123]: (root) CMD (... your command here ...)
Are you on a Mac?
If you're not seeing output in the syslog, and you're on a Mac, you may want to read about the Mac OSX switching from cron to launchd.
See the cron plist (/System/Library/LaunchDaemons/com.vix.cron.plist) and use a stdout/stderr path to debug cron itself. I don't recall if launchctl unloading and launchctl loading the plist is sufficient, or since it's a system daemon if you'd have to restart entirely. (see where is the cron log file in lion)
How to log relative to Rails?
To put the log relative to a Rails app, omit the leading slash (and typically call it cron.log)
set :output, "log/cron.log"
To put the log in a specific fully-qualified directory:
set :output, '/abc/def/ghi/log/cron.log'
The Whenever wiki has some good examples about redirecting output:
https://github.com/javan/whenever/wiki/Output-redirection-aka-logging-your-cron-jobs
Examples:
every 3.hours do
runner "MyModel.some_process", :output => 'cron.log'
rake "my:rake:task", :output => {:error => 'error.log', :standard => 'cron.log'}
command "/usr/bin/cmd"
end

Resources