cron job can't source functions from bashrc - cron

crontab -e:
30 22 * * 7 ~/bin/cron_run.sh
~/bin/cron_run.sh:
#!/bin/bash
source ~/.bashrc
#run_this_function is defined in a script sourced by bashrc
run_this_function
run_this_function is not found. What is happening here?

Try changing the source ~/.bashrc to . ~/.bashrc
That change will reload the contents of bashrc.
Then run_this_function should be available... if it is assigned properly as an alias.
Do you have the run_this_function declaration? - please post if you can.

Related

crontab does not execute script but it executable manually

Want to schedule a sh-script but crontab does not execute it.
However, the script is executable manually without any issues.
"crontab -l"-Output:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
48 16 * * 1,2,3,4,5 /home/pentaho_admin/pdi/schedule.sh
the shell script included a command with the following command in the 2nd line:
./kitchen.sh
1st line:
alias xyz="cd..." (leads to the folder of kitchen.sh)
That first line apparently cannot be interpreted by crontab.
Solution:
I've added the full path to kitchen.sh
Is there any more sexy solution?

Bash cannot set environment variable

I have this in my crontab:
* * * * * cd /etc && . ./cron.sh>>cron.log
In my cron.sh (which is executable) I have:
#!/bin/sh
echo "hello world"
export MyVar="abcd"
It runs both with cron and manually, however the environment variable is only set when I run it manually with the command:
. ./cron.sh
Can anyone please help. I know its something to do with source but I cant figure it out.
This does not work either:
* * * * * cd /etc && sh ./cron.sh>>cron.log
. will export variables in the current shell, which is the one spawned by the cron, not yours.
If you want to add an extra variable to your shells, use the ~/.profile et al (specifically the /etc/profile that is shared by all users).
I'm trying to set a variable for all users
You cannot do that via a cron job.
In fact, in general, you can't do it at all. The environment variables of a shell cannot be set from outside the shell. The UNIX / Linux operating system architecture doesn't allow it.
You could could set an environment variable for all users via /etc/profile except ...
the /etc/profile file is only executed when a user logs in, and
the user can override any environment variables set there.

Bash script to go to directory and perform commands gives "Command not found" [duplicate]

This question already has answers here:
How to use aliases defined in .bashrc in other scripts?
(6 answers)
Closed 2 years ago.
My alias defined in a sample shell script is not working. And I am new to Linux Shell Scripting.
Below is the sample shell file
#!/bin/sh
echo "Setting Sample aliases ..."
alias xyz="cd /home/usr/src/xyz"
echo "Setting done ..."
On executing this script, I can see the echo messages. But if I execute the alias command, I see the below error
xyz: command not found
am I missing something ?
source your script, don't execute it like ./foo.sh or sh foo.sh
If you execute your script like that, it is running in sub-shell, not your current.
source foo.sh
would work for you.
You need to set a specific option to do so, expand_aliases:
shopt -s expand_aliases
Example:
# With option
$ cat a
#!/bin/bash
shopt -s expand_aliases
alias a="echo b"
type a
a
$ ./a
# a is aliased to 'echo b'
b
# Without option
$ cat a
#!/bin/bash
alias a="echo b"
type a
a
$ ./a
./a: line 3: type: a: not found
./a: line 4: a: command not found
reference: https://unix.stackexchange.com/a/1498/27031 and https://askubuntu.com/a/98786/127746
sourcing the script source script.sh
./script.sh will be executed in a sub-shell and the changes made apply only the to sub-shell. Once the command terminates, the sub-shell goes and so do the changes.
OR
HACK: Simply run following command on shell and then execute the script.
alias xyz="cd /home/usr/src/xyz"
./script.sh
To unalias use following on shell prompt
unalias xyz
If you execute it in a script, the alias will be over by the time the script finishes executing.
In case you want it to be permanent:
Your alias is well defined, but you have to store it in ~/.bashrc, not in a shell script.
Add it to that file and then source it with . .bashrc - it will load the file so that alias will be possible to use.
In case you want it to be used just in current session:
Just write it in your console prompt.
$ aa
The program 'aa' is currently not installed. ...
$
$ alias aa="echo hello"
$
$ aa
hello
$
Also: From Kent answer we can see that you can also source it by source your_file. In that case you do not need to use a shell script, just a normal file will make it.
You may use the below command.
shopt -s expand_aliases
source ~/.bashrc
eval $command
Your alias has to be in your .profile file not in your script if you are calling it on the prompt.
If you put an alias in your script then you have to call it within your script.
Source the file is the correct answer when trying to run a script that inside has an alias.
source yourscript.sh
Put your alias in a file call ~/.bash_aliases and then, on many distributions, it will get loaded automatically, no need to manually run the source command to load it.

crontab output nothing when calling a .sh that contains an output

I have the following test.sh file in /home/me folder
#!/bin/sh
_now=$(date +"%Y_%m_%d")
_file="/home/me/$_now.txt"
speedtest-cli --simple > $_file
Where speedtest-cli is a python script that gives internet up and dll speed infos : https://github.com/sivel/speedtest-cli.
Calling test.sh from /home/me works very good: I get my yyy_mm_dd.txt output with all infos (dll speed up speed, etc.).
But when I try to call the test.sh from a crontab I get a empty yyy_mm_dd.txt file (nothing inside).
Inside crontab-e
20 20 * * * /home/me/test.sh
Did I do something wrong?
I suspect a PATH problem, so
pick one of :
add PATH=/usr/local/bin:/bin:/usr/bin in the top of your script
add in the top of crontab -e : PATH=/usr/local/bin:/bin:/usr/bin on his own line
source ~/.bashrc in the top of your script
add full path to each commands in your script
Your PATH is probably different for your interactive shell than the context your cronjob runs in, so you should specify the full path of speedtest-cli in your crontab entry.

CRON JOB ERROR /usr/bin/env: node: No such file or directory

i was getting this error in cron job
/usr/bin/env: node: No such file or directory
so i did this
*/10 * * * * . $HOME/.bashrc sh /path/to/cronjob.sh
in my cron job
but then it gives me this error
/etc/cron.daily/man-db:
/usr/bin/mandb: can't set the locale; make sure $LC_* and $LANG are correct
When you are doing this directly in the cron entry:
. $HOME/.bashrc you are actually asking the user cron to set its environment and most likely it has no Locale defined.
You should set your environment in your script directly just after setting the bash directive:
#!/bin/bash
. $HOME/.bashrc
echo Hello World
It would be better if you configure .bash_profile to load .profile and then load .bashrc

Resources