LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-03-2018, 01:54 PM   #1
Cabon
LQ Newbie
 
Registered: Oct 2018
Location: Chicago
Posts: 1

Rep: Reputation: Disabled
Want to set the PATH to run a SAS command from cron


Hi,

We're using Linux 3.10.0-693.17.1.el7.x86_64.

I'm trying to kick off a SAS job from cron. I understand cron is a differnt shell than my current session.

Here is my cron line below:

Code:
15 * 1 10 * /usr/bin/bash;. ~/.profile;/home/rscheid/first.bsh cat dog > /home/rscheid/first.out 2>&1
59 3 26 9 * sas -noterminal -batch /sasdata/mids_output02/rjs/simple_run.sas > /sasdata/mids_output02/rjs/simple_run.log
The second cron command just tries to run the SAS command but I think it doesn't work because in cron you lose your PATH am I right?

So I'm using the first cron line to see how I can set the PATH in a cron line. I'm not expecting it to 'carry over' to the second line I just want to see if I can get it to work on its own line. I tried to set the PATH in the first cron line by setting the shell to bash (/usr/bin/bash) then running a .profile script that just set the path to my current session's path (the .profile script is just this line--->PATH= $PATH:/usr/local/bin: /usr/bin:....) I'm using the ';' so I can have multiple commands on the same line.

When I look at the log afterwards I just see the base path which is: /usr/bin:/bin so the assignment didn't work.

I've seen this done.

How can I set the PATH from within cron?
 
Old 10-03-2018, 02:15 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,157

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
The simple way is to have cron run a script whose first line is:

Code:
#!/bin/bash
and second line is
Code:
PATH=/usr/lib64/ccache:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
or whatever.
 
Old 10-03-2018, 02:20 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Welcome to LQ!

In the cron interface you will need to set the desired path,like so (this is an example)
Code:
This has the username field, as used by /etc/crontab.
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
See also https://stackoverflow.com/questions/...-paths#2409369

Looks funny from here. Is that a test of some kind?
Code:
15 * 1 10 * /usr/bin/bash;. ~/.profile;/home/rscheid/first.bsh cat dog > /home/rscheid/first.out 2>&1
 
Old 10-03-2018, 03:05 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,748

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by Cabon View Post
Code:
15 * 1 10 * /usr/bin/bash;. ~/.profile;/home/rscheid/first.bsh cat dog > /home/rscheid/first.out 2>&1
Both of the solutions posted by smallpond and Habitual are complete and correct.
smallpond's will allow you set a path for all commands in the script.
Habitual's will set the path for all cron jobs.

To your specifics:
Each cron job will run in its own shell instance, so even if the above line did set a PATH, it wouldn't do it for the second job. Additionally the twiddle '~' is a shortcut for the $USER, and, unless specified*, there's no $USER (just like there's no $PATH)

*a user can be specified in the crontab; see man crontab for the syntax
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron Job Set to Run as User, Running with User as Part of the Command avalenti3 Linux - Server 8 02-03-2017 12:36 PM
how to set cron job to run every Friday at 09PM? paulus89 Linux - Newbie 1 07-08-2014 03:59 AM
set cron to run job every 30hours sandy_linux Linux - Newbie 5 09-02-2011 08:38 AM
How to set up a Cron Job to run every other week or every three months etc. kckabobus Linux - Newbie 3 04-03-2008 01:36 PM
how to set cron job to run every 5 mins? gsbarry Linux - General 7 05-28-2003 12:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration