How can I set for example with cronjob
1/* * * * * bash -c 'export TZ=Europe/London'
1/* * * * * bash -c '. /path/to/script.sh'
This script has multiple exports which is getting the timezone offset and would like that cronjob update the system var TZ with that one.
If I run it from shell it works with
". /path/to/script.th"
as source but not in a cronjob.
At least how to set the environment variables with cronjob.
I also have tried that the script writes into the /etc/environment
TZ=Europe/London
and cronjob to set -a;. /etc/environment; set a;
None are working but they do from shell.
Here is my script. What are my options from here when the script is ran by cronjob or anything else...Also tried to make it run with supervisord...same result.
Everything is working running locally from shell, but can't figure out how to modify the system "$TZ" environment with a script or from cronjob.
The script writes into the /etc/environment but when I set it from cron still won't update but it will from shell.
I know it all works from current shell but /etc/environment should be available to all.
I have edited so the way to go is write with the script into the /etc/profile.d/custom.sh
This works also inside a docker container to update periodicaly environmental variable if needed.
#!/bin/bash
#######################
## Preload variables ##
#######################
. /etc/profile
. ~/.bash_profile
. ~/.bashrc
#############################
## Get Boardtime from API ##
#############################
boardtime=$(curl -s http://server/api/get-time \
-u user:pass \
-d locale='en_US' | \
grep -oP '(?<=boardTime":")[^","boardTimeLocalized"]*' | \
sed -r -e 's/^.{8}/& /' -e 's/[^ ]{2}/&:/5g' | \
sed 's/:$//')
formatted_boardtime=$(date -d "$boardtime" " %a, %e %b %Y %H:%M:%S %z")
echo -ne "\nLocaltime is $formatted_boardtime!\n"
###################################
## Get timezone offset from ship ##
###################################
minutes=$(curl -s http://server/timezone/currentOffset.txt | grep -oP "[- ][0-9]{0,9}/|[0-9]{0,9}")
echo -ne "\nTimezone offset is $minutes\n"
((h=$minutes / 60))
#############################
## Set Offset Symbol Value ##
#############################
symbol=$(echo $minutes | grep -oP "[-]")
digit=$(echo $h | grep -oP "[0-9]{2,4}")
if [[ $symbol == "-" ]] && [[ $digit ]]; then
offset="-"$h"00"
elif [[ $symbol != "-" ]] && [[ $digit ]]; then
offset=" "$h"00"
elif [[ $symbol == "-" ]] && [[ ! $digit ]]; then
offset="-0$(echo "$h" | grep -oP '[0-9]{0,4}')00"
else
offset=" 0$(echo "$h" | grep -oP '[0-9]{0,4}')00"
fi
##########################
## Set Current timezone ##
##########################
localtime=$(echo $formatted_boardtime | sed -E "s/([- ][0-9]{4})$/"$offset"/")
if [[ $symbol == "-" && $offset == -1100 ]]; then
export TZ="US/Samoa"
elif [[ $symbol == "-" && $offset == -1000 ]]; then
export TZ="Pacific/Honolulu"
elif [[ $symbol == "-" && $offset == -0900 ]]; then
export TZ="America/Nome"
elif [[ $symbol == "-" && $offset == -0800 ]]; then
export TZ="America/Los_Angeles"
elif [[ $symbol == "-" && $offset == -0700 ]]; then
export TZ="America/Denver"
elif [[ $symbol == "-" && $offset == -0600 ]]; then
export TZ="America/Cancun"
elif [[ $symbol == "-" && $offset == -0500 ]]; then
export TZ="America/Lima"
elif [[ $symbol == "-" && $offset == -0400 ]]; then
export TZ="America/Santiago"
elif [[ $symbol == "-" && $offset == -0300 ]]; then
export TZ="America/Bahia"
elif [[ $symbol == "-" && $offset == -0200 ]]; then
export TZ="America/Noronha"
elif [[ $symbol == "-" && $offset == -0100 ]]; then
export TZ="Atlantic/Azores"
elif [[ $symbol != "-" && $offset == 0000 ]]; then
export TZ="Europe/London"
elif [[ $symbol != "-" && $offset == 0100 ]]; then
export TZ="Europe/Berlin"
elif [[ $symbol != "-" && $offset == 0200 ]]; then
export TZ="Europe/Athens"
elif [[ $symbol != "-" && $offset == 0300 ]]; then
export TZ="Asia/Qatar"
elif [[ $symbol != "-" && $offset == 0400 ]]; then
export TZ="Asia/Dubai"
elif [[ $symbol != "-" && $offset == 0500 ]]; then
export TZ="Indian/Maldives"
elif [[ $symbol != "-" && $offset == 0600 ]]; then
export TZ="Asia/Thimbu"
elif [[ $symbol != "-" && $offset == 0700 ]]; then
export TZ="Asia/Bangkok"
elif [[ $symbol != "-" && $offset == 0800 ]]; then
export TZ="Asia/Hong_Kong"
elif [[ $symbol != "-" && $offset == 0900 ]]; then
export TZ="Asia/Tokyo"
elif [[ $symbol != "-" && $offset == 1000 ]]; then
export TZ="Australia/Melbourne"
elif [[ $symbol != "-" && $offset == 1100 ]]; then
export TZ="Pacific/Ponape"
elif [[ $symbol != "-" && $offset == 1200 ]]; then
export TZ="Pacific/Fiji"
elif [[ $symbol != "-" && $offset == 1300 ]]; then
export TZ="Pacific/Apia"
elif [[ $symbol != "-" && $offset == 1400 ]]; then
export TZ="Pacific/Kiritimati"
fi
echo -ne "\nLocaltime is $localtime!\n"
echo -ne "\nTimezone matching offset is set $TZ!\n"
echo "$TZ" > /etc/timezone
echo "TZ=$TZ" > /etc/profile.d/custom.sh
###########################################################
## Restart postfix to apply new timezone also to postfix ##
###########################################################
/usr/sbin/postfix stop && /usr/sbin/postfix start
CodePudding user response:
Can mark as a working solution for this using as follows then:
The solution is create a /etc/profile.d/custom.sh
So I am writting in there with the script the that is ran by a cronjob and it will work also as a non-login also. Inside there simply add all the environments you need like
export TZ=Europe/London
export MYENV=value
export etc..
..