Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
05-16-2017, 09:48 AM
|
#1
|
LQ Newbie
Registered: May 2017
Posts: 8
Rep: 
|
why file is getting empty when script is running through crontab
Hello all ,
Below is my script i have passed my password in script itself. when i am running this script manually it is running good(got file on mail) but when i am running through crontab i am getting empty file on mail. could you some one tell me what can i do to resolve issue. when i am running script it is asking password but after 3 seconds it is taking password by itself.
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
cd /sasdev
echo Bajrang!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
cd
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
rm sasdevweekly.doc
|
|
|
05-16-2017, 10:03 AM
|
#2
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
after 3 seconds it might just be trying to or is running it without your pass word therefore coming up empty. If it is on your personal PC and no one can get to your script but you. adding your pass word to it maybe what is needed to be done if ran in auto mode and a password has to be used to get it to do what you want it to do.
|
|
|
05-16-2017, 10:15 AM
|
#3
|
LQ Newbie
Registered: May 2017
Posts: 8
Original Poster
Rep: 
|
Thank you sir for very quick reply, i dint get exactly what i should do here for script working properly.yes it is right ,iy is running it without your pass word therefore coming up empty. what solution i can do here . please suggest. is there any different way to provide password in script itself
|
|
|
05-16-2017, 10:22 AM
|
#4
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,468
|
Quote:
Originally Posted by Vpawar
Hello all ,
Below is my script i have passed my password in script itself. when i am running this script manually it is running good(got file on mail) but when i am running through crontab i am getting empty file on mail. could you some one tell me what can i do to resolve issue. when i am running script it is asking password but after 3 seconds it is taking password by itself.
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
cd /sasdev
echo Bajrang!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
cd
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
rm sasdevweekly.doc
|
Please use CODE tags when posting scripts.
I've bolded a line in the script above. When you run the script manually, you're running it as your logged-in user, so when you type in "cd", it returns to your home directory. When you run from CRON, it is NOT running as your user ID, but rather under the cron daemon. Put an entire path in the cd line, and also fully qualify the rm line with a complete path.
Aside from that, when you're running sudo, it gets the UID from the logged in session...manually, it's you. Through CRON?? It has no idea....read the man page on the sudo command, and pay particular attention to the -U flag. But is this cron job in your users crontab, or in the root crontab??? If it's in root....why use sudo at all, since that means the commands will run as root anyway, and not NEED sudo at all.
Last edited by TB0ne; 05-16-2017 at 10:26 AM.
|
|
2 members found this post helpful.
|
05-16-2017, 11:17 AM
|
#5
|
LQ Newbie
Registered: May 2017
Posts: 8
Original Poster
Rep: 
|
still i am facing same problem, please suggest is there any different way to provide password to sudo from script itself or how can do this script password free or if any output is getting on putty screen how can take it on my mail directly.
Note:- i do not have root access
|
|
|
05-16-2017, 11:23 AM
|
#6
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,853
|
Quote:
Originally Posted by TB0ne
Put an entire path in the cd line, and also fully qualify the rm line with a complete path.
|
To second that comment: As a Best Practice, when developing scripts, ALWAYS use absolute paths to other scripts, commands and files.
example: use
Code:
/bin/sort [which sort], not just sort
/bin/mailx intead of mailx
/home/vpawar/sasdevweekly.doc instead of cd and sasdevweekly.doc
I don't want to admit the number of times I've struggled with a script not working because it didn't know where find or ls was...
--
Sean
|
|
1 members found this post helpful.
|
05-16-2017, 11:24 AM
|
#7
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by Vpawar
still i am facing same problem, please suggest is there any different way to provide password to sudo from script itself or how can do this script password free or if any output is getting on putty screen how can take it on my mail directly.
Note:- i do not have root access
|
I think answering @TB0ne Questions might help him help you a LOT.
|
|
1 members found this post helpful.
|
05-16-2017, 11:28 AM
|
#8
|
Senior Member
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375
|
Seems to be a lot of inconsistency here over where sasdevweekly.doc is located.
puts you in /sasdev
Code:
> /home/vpawar/sasdevweekly.doc
overwrites/creates /home/vpawar/sasdevweekly.doc
Code:
-a sasdevweekly.doc
is looking for /sasdev/sasdevweekly.doc
Code:
rm sasdevweekly.doc
is looking to delete /sasdev/sasdevweekly.doc
|
|
1 members found this post helpful.
|
05-16-2017, 11:30 AM
|
#9
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,468
|
Quote:
Originally Posted by Vpawar
still i am facing same problem, please suggest is there any different way to provide password to sudo from script itself or how can do this script password free or if any output is getting on putty screen how can take it on my mail directly.
Note:- i do not have root access
|
Answer the questions asked, and we may be able to help. Read the man page on the sudo command. And read the "Question Guidelines" link in my posting signature...unless you ask a clear question (and this post is NOT clear), we can't help.
And saying that you don't have root access when you DO have sudo access is very odd. You do realize that sudo is used to give 'regular' users root-level access, right??? You do know what sudo does?
|
|
|
05-16-2017, 12:51 PM
|
#10
|
LQ Newbie
Registered: May 2017
Posts: 8
Original Poster
Rep: 
|
sir the problem is i cant create any file in
that is why i created file on local folder
Quote:
/home/vpawar/sasdevweekly.doc
|
second thing is when i am using super user do (sudo ) system is asking me password, That is why i passed password in script itself.
i used absolute path where you guys have suggested but it is not heading to resolution of my problem.
when i am running it manually it is successfully doing it. is not running from my user id it is running through sudo access so it is asking me password . but through crontab it is sending empty file to me.
|
|
|
05-16-2017, 01:39 PM
|
#11
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by Vpawar
sir the problem is i cant create any file in
that is why i created file on local folder
second thing is when i am using super user do (sudo ) system is asking me password, That is why i passed password in script itself.
i used absolute path where you guys have suggested but it is not heading to resolution of my problem.
when i am running it manually it is successfully doing it. is not running from my user id it is running through sudo access so it is asking me password . but through crontab it is sending empty file to me.
|
1. you never gave an answer to which side you are running it on, user or root.
if everything is on the root side then run it on root side. su password then set it up in cron.
or side step the password by changing your sudoers file to NOPASSWD for sudo group.
that is what I'd do and I do not even use cron. (well one file)
|
|
|
05-16-2017, 01:53 PM
|
#12
|
LQ Newbie
Registered: May 2017
Posts: 8
Original Poster
Rep: 
|
I am running on user side.
|
|
|
05-16-2017, 02:07 PM
|
#13
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Yours
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
cd /sasdev
echo Bajrang!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
cd
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
rm sasdevweekly.doc
why not do this on root side? My rewrite.
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
#cd /sasdev <-- I assume that is a dir to that file below. if that is a file.
#why not just do it like this?
echo /sasdev/Bajrang!1 | /usr/bin/du /sasdev -hsbx *|sort -rh > /tmp/sasdevweekly.doc
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a
/tmp/sasdevweekly.doc vpawar@gmail.com
rm /tmp/sasdevweekly.doc
root can do whatever it wants - so doing this on root side to me seems more logical.
I have no idea if that rewrite works but theoretically it should.
Last edited by BW-userx; 05-16-2017 at 02:15 PM.
|
|
2 members found this post helpful.
|
05-17-2017, 06:52 AM
|
#14
|
LQ Newbie
Registered: May 2017
Posts: 8
Original Poster
Rep: 
|
Thank you sir . I really appreciate your try for resolve this, let me give you explanation for every line
I am getting in SASDEV volumn, because i need to run below commands on SASDEV volumn.
Quote:
echo passwrd!1 | sudo -S /usr/bin/du /sasdev -hsbx *|sort -rh > /home/vpawar/sasdevweekly.doc
|
In above line i am passing passwrd!1 as a password because when i am executing only
Quote:
sudo /usr/bin/du /sasdev -hsbx *|sort -rh
|
this commands, system is asking me password so i am passing it in script itself and taking that output in sasdevweekly.doc
Note: i cant create file on SASDEV volumn, that is why i am creating file on my local folder(/home/vpawar).
getting in my local folder.
Quote:
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a sasdevweekly.doc vpawar@gmail.com
|
here, i am sending sasdevweekly.doc file on my mailbox
when i am running this script manually it is taking password from script itself and sending out file successfully,but when i put it on cron it is not able to take that password and sending empty file.
|
|
|
05-17-2017, 09:36 AM
|
#15
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
I'd get rid of that password needed problem by and if necessary set root up with a new/different mail account that root use. If that is needed for the report to get emailed out.
Because I do not see where it matters who sends it. A user or root user, as long as it gets sent.
This way being that /sasdev is indicating that it is on the root side of the system. Then Logically it should be Root user should be taking care of business. Just remove the sudo part. I do think your system should allow a sudo user to log in a terminal as su or su - to gain root privileges. Then set up your CRON job under root user, so that CRON will see it as root requesting this job to be ran. No password needed. that has now been eliminated. easy peasy. done.
Code:
#!/bin/ksh
# A script to evalute and send email with required information for SASprod weekly report
/usr/bin/du /sasdev -hsbx *|sort -rh > /tmp/sasdevweekly.doc
echo "This file for sasprod weekly report, which sent by automated script" | mailx -s "List of files" -a
/tmp/sasdevweekly.doc vpawar@gmail.com
rm /tmp/sasdevweekly.doc
Like @TB0ne questioned. Is this being ran on user side or Root side? Bang Really Big  went off in my head, and I do not use CRON. Just applied methodology on how to get things done on the root side of the system.
|
|
|
All times are GMT -5. The time now is 11:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|