Link to home
Start Free TrialLog in
Avatar of MIHIR KAR
MIHIR KARFlag for India

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-Files/ScheduleDevice.sh
---------
#!/bin/bash

java -cp ".:mysql-connector (1).jar:org.eclipse.paho.client.mqttv3-1.1.0.jar" ScheduleDevice

the_output=`java -cp ".:mysql-connector (1).jar:org.eclipse.paho.client.mqttv3-1.1.0.jar" ScheduleDevice`

echo "$the_output"

while read line; do echo "LINE: '${line}'"; done < <(echo "$the_output")

crontab -l
--------------
*/5 * * * * /usr/local/cronjobs/Java-Files/ScheduleDevice.sh >> /usr/local/cronjobs/Java-Files/ScheduleDevice.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'
Avatar of tel2
tel2
Flag of New Zealand image

Maybe the script puts some output to STDERR instead of STDOUT.
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-Files/ScheduleDevice.sh >> /usr/local/cronjobs/Java-Files/ScheduleDevice.log 2&>1
Avatar of MIHIR KAR

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-Files/ScheduleDevice.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
SOLUTION
Avatar of tel2
tel2
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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-Files/ScheduleDevice.log
   */5 * * * * /usr/local/cronjobs/Java-Files/ScheduleDevice1.sh >> /usr/local/cronjobs/Java-Files/ScheduleDevice.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-Files -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-Files/stdout.txt | while read LINE
do
echo $LINE
done
rturncd=$?
echo "Script ended with exit code $rturncd"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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-Files/ScheduleDevice.sh

#!/bin/bash
export JAVA_HOME=/usr
export SCRIPTS_PATH=/usr/local/cronjobs/Java-Files
export PATH=$JAVA_HOME/bin:$PATH
export PATH=$SCRIPTS_PATH:$PATH

java -cp "/usr/local/cronjobs/Java-Files:/usr/local/cronjobs/Java-Files/mysql-connector (1).jar:/usr/local/cronjobs/Java-Files/org.eclipse.paho.client.mqttv3-1.1.0.jar" ScheduleDevice



Script 2
---------
/usr/local/cronjobs/Java-Files/ScheduleDevice1.sh

#! /bin/bash

echo "removeing files"

files=`find /usr/local/cronjobs/Java-Files -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-Files/ScheduleDevice.sh >/usr/local/cronjobs/Java-Files/stdout.txt 2>/usr/local/cronjobs/Java-Files/stderr.txt

#/usr/bin/java -cp ".:/usr/local/cronjobs/Java-Files/mysql-connector (1).jar:/usr/local/cronjobs/Java-Files/org.eclipse.paho.client.mqttv3-1.1.0.jar" ScheduleDevice

set +x

cat /usr/local/cronjobs/Java-Files/stdout.txt | while read LINE
do
echo $LINE
done

rturncd=$?
echo "Script ended with exit code $returncd"

 Cron Job
-------------
*/5 * * * * sh /usr/local/cronjobs/Java-Files/ScheduleDevice1.sh >> /usr/local/cronjobs/Java-Files/ScheduleDevice.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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Simon.
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-Files -type f -name "std*.txt"`

tel2