Execute several instances of script same time - bash

I have a crontable that executes a script with different args
0,5,10 * * * * /path/to/logScript "script2"
0,5,10 * * * * /path/to/logScript "script1"
0,5,10 * * * * /path/to/logScript "script3"
script
#!/bin/bash
script=$1
$script args >/path/tmp/log.out 2>/path/tmp/log.err
if [ ! -s /path/tmp/log.err ]; then
echo -n "$(date) success: " >> /path/logfile
cat /path/tmp/log.out >> /path/logfile
else
echo -n "$(date) errors: " >> /path/logfile
cat /path/tmp/log.err >> /path/logfile
fi
The issue that I think i'm having is with the execution of the script in the same time. The script doen't get the right value of return (to know wheter it's stderr or stdout). If i execute the crontable lines one after one in a terminal it works fine but when it's executed automatically the data i get is incorrect.
I tried to solve this by making this changement to crontable but i still have the same issue.
0,5,10 * * * * /path/to/logScript "script2"&
0,5,10 * * * * /path/to/logScript "script1"&
0,5,10 * * * * /path/to/logScript "script3"&

if all commands are running at same time, think you could combine the command in a unique one, using & at end of commands to push them to background mode, like this:
0,5,10 * * * * /bin/bash -c '/path/to/logScript "script2" & /path/to/logScript "script1" & /path/to/logScript "script3" & '

you need to run it as a background process (add "&" at the end of the process line so you can "spawn" them)
you can also wrap it into a loop with i being number of processes and run it within the same file.
func () {
#do this
#do that
#hit him with a bat
}
then in sh
#!/bin/bash
for i in $(seq 0 5);
do
function() &
echo "child pid: " $!
done

Related

How to add one more command in crontab?

How can I add some commands in crontab?
0 * * * * cd /var/www/cron && /usr/loc/php artisan schedule:run 2>&1 >> /var/log/logs.log
0 5 * * * cd /var/www/cron && /usr/loc/php artisan present:run 2>&1 >> /var/log/present.log
You can add new line in crone tab as below
* * * * * cd /var/www/cron && /usr/loc/php artisan mail:send 2>&1 >> /var/log/mail.log

Task scheduling No command 'app' found, but there are 16 similar ones

I was trying to schedule automated tasks using but nothing would happen
* * * * * root /usr/bin/php /home/user/laravel/artisan schedule:run >> /home/user/cron.log 2>&1
So I tried calling the job directly using the command below
* * * * * root /usr/bin/php /home/user/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
After I enter the command above I keep this error in my cron.log
No command 'app' found, but there are 16 similar ones
app: command not found
If I run php aritsan email:panelReport I recieve the email just fine.
Kernel.php
protected $commands = [
Commands\EmailPanelReport::class
];
protected function schedule(Schedule $schedule)
{
$schedule
->command('email:panelReport')
->everyMinute()
}
EmailPanelReport.php
protected $signature = 'email:panelReport';
protected $description = 'Send out weekly report';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$panels = Orders::where('orders.prefix', 'P')->get();
$columns = array ('Date', 'Order Number', 'Description');
if ( !ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}
try
{
$csv = Writer::createFromFileObject(new SplTempFileObject());
$csv->insertOne($columns);
$csv->insertAll($panels->toArray());
$output = $csv->getContent();
Mail::raw('See attached', function($message) use ($output)
{
$message->to('test#gmail.com');
$message->subject("test");
$message->attachData($output, 'test.csv', [
'mime' => 'text/csv',
]);
});
$this->info("local");
}
catch (\Exception $ex)
{
$this->error($ex->getMessage());
Mail::raw($ex->getMessage(), function($message)
{
$message->to('myemail#me.com');
});
}
}
System info:
Ubuntu 16.04
nginx/1.10.3
php 7.1.7
Other ways I have tried
* * * * * root /usr/bin/php /home/user/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
* * * * * root /usr/bin/php usr/share/nginx/www/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
* * * * * /usr/bin/php /home/user/laravel/artisan email:panelReport >> /home/user/cron.log 2>&1
* * * * * root /usr/bin/php /home/user/laravel/artisan schedule:run >> /home/user/cron.log 2>&1
You can schedule it writing your command using the crontab editor:
crontab -e
And paste your command on end of file, exit and save. The results should looks like this:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
To see your scheduled commands run:
crontab -l
I hope it helps!

Crontab run a ruby file not work

*/1 * * * * ~/.rbenv/versions/2.1.5/bin/ruby /Users/zhangjian/Desktop/mail.rb
*/1 * * * * /usr/bin/ruby /Users/zhangjian/Desktop/OHS_Project/ohs_server/rest/notification.rb
I write these 2 under crontab e but it doesn't work. I try run the file directively, everything works.
aFile = File.new("./time.txt", "a+")
if aFile
aFile.syswrite(Time.new.inspect)
aFile.syswrite(" ")
else
puts "Unable to open file!"
end
how can I fix it?
Try using the full path without ~ in the crontab. You should also try to use an absolute path when referencing time.txt.

Path not get set from crontab

Very simple and yet not working. Path not get set.
crontab:
* * * * * source /home/inst1/.profile; /home/inst1/Scripts/test.sh > /home/inst1/Scripts/test.log 2>&1
.profile:
[..whatever..]
PATH=/tmp
export PATH
test.sh:
#!/usr/bin/bash
echo $PATH
cat to test.log gives me:
/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java5/jre/bin:/usr/java5/bin
I guess it related to AIX environment, but I have no idea - seems simple but I'm missing something somewhere.
Try this:
* * * * * bash -c "source /home/inst1/.profile; /home/inst1/Scripts/test.sh" > /home/inst1/Scripts/test.log 2>&1
cron uses /bin/sh by default, while source is not a POSIX shell command. You need to use . instead:
* * * * * . /home/inst1/.profile; /home/inst1/Scripts/test.sh > /home/inst1/Scripts/test.log 2>&1

Insert entry into crontab unless it already exists (as one-liner if possible) [duplicate]

This question already has answers here:
How can I programmatically create a new cron job?
(20 answers)
Closed 4 years ago.
What's the preferred method to insert an entry into /etc/crontab unless it exists, preferably using a one-liner?
Here's my example entry I wish to place into /etc/crontab unless it already exists in there.
*/1 * * * * some_user python /mount/share/script.py
I'm on CentOS 6.6 and so far I have this:
if grep "*/1 * * * * some_user python /mount/share/script.py" /etc/crontab; then echo "Entry already in crontab"; else echo "*/1 * * * * some_user python /mount/share/script.py" >> /etc/crontab; fi
You can do this:
grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 * * * * some_user python /mount/share/script.py' >> /etc/crontab
If the line is absent, grep will return 1, so the right hand side of the or || will be executed.
Factoring out the filename & using the q & F options
file="/etc/crontab"; grep -qF "some_user python /mount/share/script.py" "$file" || echo "*/1 * * * * some_user python /mount/share/script.py"
You can do it like this:
if grep "\*\/5 \* \* \* \* /usr/local/bin/test.sh" /var/spool/cron/root; then echo "Entry already in crontab"; else echo "*/5 * * * * /usr/local/bin/test.sh" >> /var/spool/cron/root; fi
Or even more terse:
grep '\*\/12 \* \* \* \* /bin/yum makecache fast' /var/spool/cron/root \
|| echo '*/12 * * * * /bin/yum makecache fast' >> /var/spool/cron/root

Resources