I have problem with environment variables in docker image..
I created file: /cron.sh
#!/bin/bash
whoami
export
When I run manual in console: sh /cron.sh is OK (root, full envs) but when I add command to cron that I getting: root, basic envs (not have my variables).
Crontab -e:
* * * * * /bin/bash /cron.sh > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1
Why are the differences?
I have solution..
before run "exec crond -n" in entrypoint I save environments to file.
export > /.env
Next in cron add:
* * * * * source /.env; /bin/bash /cron.sh > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1
I hope I helped :-)
Related
I created this test bash script
#! /bin/bash
echo "hello"
mkdir "/karan/washere"
cron job i created, i want to run this cron job to run every min and want the log
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
I am signed in the droplet as root user
I also have given the permission for the script, using
sudo chmod u+x test.sh
I tried to log the syslog using
sudo grep CRON /var/log/syslog
but didn't show there also,
let me know if you need any more info or other context,
cron uses sh to interpret the commands, not bash. sh has less features and the &> is not valid in sh.
A better way for your line in the crontab would be:
1 * * * * /user/local/bin/bash /root/test.sh >> /root/crontest.log 2>> /root/crontest.err
This will append to the logging instead of overwriting, and it will separate log from errors.
If you want, however, you can force cron to use bash instead of sh. Your crontab should then be:
SHELL=/bin/bash
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
I wanna run a script using crontab in every minute. The script is located at /Users/robin47/Desktop/script/demo.sh. As a side note, I give the execute permission of demo.sh file with chmod +x demo.sh command.
But it doesn't execute the script. Here is the process I'm following:
Run crontab -e from the terminal.
robin47#mac-pro ~ % crontab -e
this open a file(crontab.xxxxxxx) in nano which is located under tmp folder, as an example File: /tmp/crontab.V4DcXEJU8g.
This is my crontab.xxxxxxx file:
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /Users/robin47/Desktop/script/demo.sh
Get from here, this doesn't execute my demo.sh script.
If I run sh command from terminal like sh demo.sh, it works. And if I directly echo(or run any command like touch, mkdir) in crontab like this
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * echo 'hello' >> /Users/robin47/Desktop/script/text.txt
It also works. But doesn't run the script using crontab. I appreciate your help, Thanks.
This is my demo.sh file:
#!/bin/sh
echo "hello world" >> /Users/robin47/Desktop/script/text.txt
refer below site for cron entries , also check your script have executable permission
chmod oug+x demo.sh
https://crontab.guru/
We have a set of environment variables being exported from an .envrc file within a dir. In the shell you can run direnv allow and the .envrc file gets invoked, thus creating the env vars.
We would like to have this same functionality when running a script with a crontab job, but the envrc file never runs.
We have tried:
In crontab: * * * * . $HOME/.profile; some/script/to/run
In script: direnv allow
OR
In script: eval "$(direnv export bash)"
None of that works, is there a way to run the .envrc file from a script invoked by a cron job?
You can use direnv exec to achieve this. It has the the arguments [DIR] COMMAND [...ARGS]. If the .envrc file is in the same file as the script, you can change the following:
* * * * * sh /path/to/script/script.sh
To this:
* * * * * direnv exec sh /path/to/script/script.sh
If the .envrc file is in a different directory than the script, then you'll have to pass the path to the directory just after the exec argument like so:
* * * * * direnv exec /path/to/secrets sh /path/to/script/script.sh
source
I want run my script from the crontab on Mac OS, but I'm getting an error:
ruby: Operation not permitted -- /Users/vitalii/Desktop/Home/update/update.rb (LoadError)
My preferences for the cron task and settings are created using rvm cron setup:
#sm start rvm
PATH="/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/vitalii/.rvm/bin"
GEM_HOME='/Users/vitalii/.rvm/gems/ruby-2.4.1'
GEM_PATH='/Users/vitalii/.rvm/gems/ruby-2.4.1:/Users/vitalii/.rvm/gems/ruby-2.4.1#global'
MY_RUBY_HOME='/Users/vitalii/.rvm/rubies/ruby-2.4.1'
IRBRC='/Users/vitalii/.rvm/rubies/ruby-2.4.1/.irbrc'
RUBY_VERSION='ruby-2.4.1'
#sm end rvm
* * * * * ruby /Users/vitalii/Desktop/Home/update/update.rb >> /Users/vitalii/Desktop/logfile.txt 2>&1
I gave each file the rights to execute with chmod 777, but there are no changes and the error is repeated.
The contents of the file update.rb are
puts 'Hello, World!!!'
Can someone tell me what's going on and what I'm doing wrong ?
I found solution, since I use Mojave.
I need to make additional settings in the system. Who would have thought ...
enter link description here
This turns out to be a problem, since you need to allow permissions for cron.
And correct run command for crontab -e, this
* * * * * /bin/bash -l -c 'ruby /Users/vitalii/Desktop/Home/update/update.rb'
There is a little difference when setting crontab job via crontab -e or putting them in /etc/crontab
$ crontab -e
# m h dom mon dow command
* * * * * echo ok
$ nano /etc/crontab
# m h dom mon dow user command
* * * * * root echo ok
Is this the case?
I'm trying to run a cron job on macOS and a site hosted locally using MAMP.
I have tried various options, none to any avail; please see below:
*/1 * * * * php http://mylocalsite.com/cron/my_function
*/1 * * * * /usr/bin/curl –silent –compressed http://mylocalsite.com/cron/my_function
*/1 * * * * /usr/bin/curl --silent --compressed http://mylocalsite.com/cron/my_function
*/1 * * * * /Applications/MAMP/bin/php/php7.1.12/bin/php http://mylocalsite.com/cron/my_function > /dev/null 2>&1
*/1 * * * * wget --no-check-certificate -O- https://mylocalsite.com/cron/my_function >> /dev/null
When I run the following in terminal, it does work:
wget --no-check-certificate -O- https://mylocalsite.com/cron/my_function >> /dev/null
I know that the URL executes the function that I want as I have tested this directly in a browser.
What am I doing wrong and what should go into the crontab in order to ping/run the specified URL?
The CodeIgniter manual suggests that running CodeIgniter through the commandline is possible.
They write the following script:
<?php
class Tools extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
In turn they execute the script like this on the server:
$ cd /path/to/project;
$ php index.php tools message
Where tools denotes the controller, and message the action.
So in that case the crontab entry would become:
*/1 * * * * cd /path/to/project; php index.php tools message
And in the case sketched in the question:
*/1 * * * * cd /path/to/project; php index.php cron my_function
SOURCE: https://www.codeigniter.com/user_guide/general/cli.html
The thing I like to do is:
Make sure you have php installed using brew (brew install php#version (I use php#7.4))
Check if you have crontab, else install using brew install crontab
Enter crontab -l into the terminal, this will open a vim editor. If you can't seem to type anything press the i key. Then start entering the commands like :
*/1 * * * * cd /path/to/project; php index.php
press : and following it enter wq and enter to save and exit the vim, making it :wq
check cronjob using crontab -e in terminal
Additional details here(for crontab) and here(for vim)