MIHIR KAR
asked on
Shell Script Execution and format o/p error!
Hello Expert,
I want to read o/p from a java process using following cron job, when i tried to use below script its o/p not written into the ScheduleDevice.log.
Could you please guide me why it throwing empty o/p inside log.
Script /usr/local/cronjobs/Java-F iles/Sched uleDevice. sh
---------
#!/bin/bash
java -cp ".:mysql-connector (1).jar:org.eclipse.paho.c lient.mqtt v3-1.1.0.j ar" ScheduleDevice
the_output=`java -cp ".:mysql-connector (1).jar:org.eclipse.paho.c lient.mqtt v3-1.1.0.j ar" ScheduleDevice`
echo "$the_output"
while read line; do echo "LINE: '${line}'"; done < <(echo "$the_output")
crontab -l
--------------
*/5 * * * * /usr/local/cronjobs/Java-F iles/Sched uleDevice. sh >> /usr/local/cronjobs/Java-F iles/Sched uleDevice. log
ScheduleDevice.log file 0/p
LINE: ''
The Java O/P is
Script Executed Successfully at Fri Jun 29 20:50:02 IST 2018
LINE: ''
Normal o/p when executing using below script in ad-hoc
[root@vps1 Java-Files]# ./ScheduleDevice.sh
The Java O/P is
Script Executed Successfully at Fri Jun 29 20:51:56 IST 2018
Before start of all threads
2018-06-29
20:51
List of pendingSchedules
Finished all threads
Finished all threads
LINE: 'Before start of all threads'
LINE: '2018-06-29'
LINE: '20:51'
LINE: 'List of pendingSchedules'
LINE: 'Finished all threads'
LINE: 'Finished all threads'
I want to read o/p from a java process using following cron job, when i tried to use below script its o/p not written into the ScheduleDevice.log.
Could you please guide me why it throwing empty o/p inside log.
Script /usr/local/cronjobs/Java-F
---------
#!/bin/bash
java -cp ".:mysql-connector (1).jar:org.eclipse.paho.c
the_output=`java -cp ".:mysql-connector (1).jar:org.eclipse.paho.c
echo "$the_output"
while read line; do echo "LINE: '${line}'"; done < <(echo "$the_output")
crontab -l
--------------
*/5 * * * * /usr/local/cronjobs/Java-F
ScheduleDevice.log file 0/p
LINE: ''
The Java O/P is
Script Executed Successfully at Fri Jun 29 20:50:02 IST 2018
LINE: ''
Normal o/p when executing using below script in ad-hoc
[root@vps1 Java-Files]# ./ScheduleDevice.sh
The Java O/P is
Script Executed Successfully at Fri Jun 29 20:51:56 IST 2018
Before start of all threads
2018-06-29
20:51
List of pendingSchedules
Finished all threads
Finished all threads
LINE: 'Before start of all threads'
LINE: '2018-06-29'
LINE: '20:51'
LINE: 'List of pendingSchedules'
LINE: 'Finished all threads'
LINE: 'Finished all threads'
ASKER
Hello @tel2 thank you for your useful tips,
I have followed your process.... But here script is executing successfully it stores the output into >stdout.txt file
Here i want to redirect the stdout.txt file o/p into /usr/local/cronjobs/Java-F iles/Sched uleDevice. log file ,when i tried to doing this with cron job it took the duplicate time and stores into log .
FYI & R -- Please find the bold time
Script Executed Successfully at Sat Jun 30 13:52:00IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
FYI & RFYI & R
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
I have followed your process.... But here script is executing successfully it stores the output into >stdout.txt file
Here i want to redirect the stdout.txt file o/p into /usr/local/cronjobs/Java-F
FYI & R -- Please find the bold time
Script Executed Successfully at Sat Jun 30 13:52:00IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
FYI & RFYI & R
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
Script ended with exit code
removeing files
File not Exists..
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
13:51
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 13:52:00 IST 2018
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you once again @tel2
Q1. Any significant differences?
Ans:- As i test the "env" command with both scenario (command-line/ inside script) there nothing changes found!
Q2, And have you actually tried putting 2&>1 at the end of the cron job command to see if that helps.
Unlikely, given what you told me, but try it and let me know what happens.
Ans:- When i tried "2&>1" with following cron job nothing is printed to /usr/local/cronjobs/Java-F iles/Sched uleDevice. log
*/5 * * * * /usr/local/cronjobs/Java-F iles/Sched uleDevice1 .sh >> /usr/local/cronjobs/Java-F iles/Sched uleDevice. log 2&>1
Also, could you please run the command I gave in my last post again, and then run:
wc std*.txt
Q3. What is the output?
[root@vps1 Java-Files]# wc std*.txt
0 0 0 stderr.txt
9 32 209 stdout.txt
10 34 225 total
Please let me know is there anything missed or need to test.
FYI..
Currently i'm executing the ad-hoc script manually it's works fine
[root@vps1 Java-Files]# ./ScheduleDevice1.sh
removeing files
File not Exists..
+ ./ScheduleDevice.sh
+ set +x
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
17:43
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 17:43:15 IST 2018
Script--/ScheduleDevice1.sh
#! /bin/bash
echo "removeing .txt files"
files=`find /usr/local/cronjobs/Java-F iles -type f -name "std*.txt"`
if [[ -f "$files" ]]
then
echo " $files found."
echo "Delete Exixting files"
rm $files
else
echo "File not Exists.."
fi
set -x
./ScheduleDevice.sh >stdout.txt 2>stderr.txt
set +x
cat /usr/local/cronjobs/Java-F iles/stdou t.txt | while read LINE
do
echo $LINE
done
rturncd=$?
echo "Script ended with exit code $rturncd"
Q1. Any significant differences?
Ans:- As i test the "env" command with both scenario (command-line/ inside script) there nothing changes found!
Q2, And have you actually tried putting 2&>1 at the end of the cron job command to see if that helps.
Unlikely, given what you told me, but try it and let me know what happens.
Ans:- When i tried "2&>1" with following cron job nothing is printed to /usr/local/cronjobs/Java-F
*/5 * * * * /usr/local/cronjobs/Java-F
Also, could you please run the command I gave in my last post again, and then run:
wc std*.txt
Q3. What is the output?
[root@vps1 Java-Files]# wc std*.txt
0 0 0 stderr.txt
9 32 209 stdout.txt
10 34 225 total
Please let me know is there anything missed or need to test.
FYI..
Currently i'm executing the ad-hoc script manually it's works fine
[root@vps1 Java-Files]# ./ScheduleDevice1.sh
removeing files
File not Exists..
+ ./ScheduleDevice.sh
+ set +x
reading contens...
The Java O/P is
Before start of all threads
2018-06-30
17:43
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Sat Jun 30 17:43:15 IST 2018
Script--/ScheduleDevice1.sh
#! /bin/bash
echo "removeing .txt files"
files=`find /usr/local/cronjobs/Java-F
if [[ -f "$files" ]]
then
echo " $files found."
echo "Delete Exixting files"
rm $files
else
echo "File not Exists.."
fi
set -x
./ScheduleDevice.sh >stdout.txt 2>stderr.txt
set +x
cat /usr/local/cronjobs/Java-F
do
echo $LINE
done
rturncd=$?
echo "Script ended with exit code $rturncd"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You Every one for your Support and Feedback! Much Appreciate
Finally my Cron job working well, i can see the Schedule-device get append with different timestamp .
FYR.. Which i'm using now as per your instruction.
Script1
------------
/usr/local/cronjobs/Java-F iles/Sched uleDevice. sh
#!/bin/bash
export JAVA_HOME=/usr
export SCRIPTS_PATH=/usr/local/cr onjobs/Jav a-Files
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$SCRIPTS_PATH:$PATH
java -cp "/usr/local/cronjobs/Java- Files:/usr /local/cro njobs/Java -Files/mys ql-connect or (1).jar:/usr/local/cronjob s/Java-Fil es/org.ecl ipse.paho. client.mqt tv3-1.1.0. jar" ScheduleDevice
Script 2
---------
/usr/local/cronjobs/Java-F iles/Sched uleDevice1 .sh
#! /bin/bash
echo "removeing files"
files=`find /usr/local/cronjobs/Java-F iles -type f -name "std*.txt"`
if [[ -f "$files" ]]
then
echo " $files found."
echo "Deleting Exixting files"
rm $files
else
echo "File not Exists.."
fi
#Calling script1 for Java o/p
set -x
sh /usr/local/cronjobs/Java-F iles/Sched uleDevice. sh >/usr/local/cronjobs/Java- Files/stdo ut.txt 2>/usr/local/cronjobs/Java -Files/std err.txt
#/usr/bin/java -cp ".:/usr/local/cronjobs/Jav a-Files/my sql-connec tor (1).jar:/usr/local/cronjob s/Java-Fil es/org.ecl ipse.paho. client.mqt tv3-1.1.0. jar" ScheduleDevice
set +x
cat /usr/local/cronjobs/Java-F iles/stdou t.txt | while read LINE
do
echo $LINE
done
rturncd=$?
echo "Script ended with exit code $returncd"
Cron Job
-------------
*/5 * * * * sh /usr/local/cronjobs/Java-F iles/Sched uleDevice1 .sh >> /usr/local/cronjobs/Java-F iles/Sched uleDevice. log
Log..
----
Script Executed Successfully at Tue Jul 3 08:38:01 IST 2018
Script ended with exit code
removeing files
File not Exists..
Before start of all threads
2018-07-03
08:39
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Tue Jul 3 08:39:02 IST 2018
Script ended with exit code
removeing files
File not Exists..
Before start of all threads
2018-07-03
08:40
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Tue Jul 3 08:40:02 IST 2018
Script ended with exit code
Thank You Once Again!
Finally my Cron job working well, i can see the Schedule-device get append with different timestamp .
FYR.. Which i'm using now as per your instruction.
Script1
------------
/usr/local/cronjobs/Java-F
#!/bin/bash
export JAVA_HOME=/usr
export SCRIPTS_PATH=/usr/local/cr
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$SCRIPTS_PATH:$PATH
java -cp "/usr/local/cronjobs/Java-
Script 2
---------
/usr/local/cronjobs/Java-F
#! /bin/bash
echo "removeing files"
files=`find /usr/local/cronjobs/Java-F
if [[ -f "$files" ]]
then
echo " $files found."
echo "Deleting Exixting files"
rm $files
else
echo "File not Exists.."
fi
#Calling script1 for Java o/p
set -x
sh /usr/local/cronjobs/Java-F
#/usr/bin/java -cp ".:/usr/local/cronjobs/Jav
set +x
cat /usr/local/cronjobs/Java-F
do
echo $LINE
done
rturncd=$?
echo "Script ended with exit code $returncd"
Cron Job
-------------
*/5 * * * * sh /usr/local/cronjobs/Java-F
Log..
----
Script Executed Successfully at Tue Jul 3 08:38:01 IST 2018
Script ended with exit code
removeing files
File not Exists..
Before start of all threads
2018-07-03
08:39
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Tue Jul 3 08:39:02 IST 2018
Script ended with exit code
removeing files
File not Exists..
Before start of all threads
2018-07-03
08:40
List of pendingSchedules
Finished all threads
Finished all threads
Script Executed Successfully at Tue Jul 3 08:40:02 IST 2018
Script ended with exit code
Thank You Once Again!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks Simon.
ASKER
Thanks a Lot @Simon and @tel, Much Appreciate for your time and valuable comments.
Hi again Mihir,
You remember when I wrote the following, above?:
As a short alternative to this code:
if [[ "$files" ]]
then
echo " $files found."
echo "Deleting Exixting files"
rm $files
else
echo "File not Exists.."
fi
you could simply use something like this one-liner:
rm -v std*.txt 2>&1
I forgot to say the following line can also be removed if you are using that one-liner:
files=`find /usr/local/cronjobs/Java-F iles -type f -name "std*.txt"`
tel2
You remember when I wrote the following, above?:
As a short alternative to this code:
if [[ "$files" ]]
then
echo " $files found."
echo "Deleting Exixting files"
rm $files
else
echo "File not Exists.."
fi
you could simply use something like this one-liner:
rm -v std*.txt 2>&1
I forgot to say the following line can also be removed if you are using that one-liner:
files=`find /usr/local/cronjobs/Java-F
tel2
To confirm that, manually run it like this as a test:
./ScheduleDevice.sh >stdout.txt 2>stderr.txt
Then show us the output of both files like this:
head std*.txt
If some of the output is in stderr.txt, you could add "2&>1" to the end of the cron job like this:
*/5 * * * * /usr/local/cronjobs/Java-F