Almost a duplicate question of this one, however, the answer in that post does not solve the environmental issue with at command.
Problem description:
I want to run my shell scripts with at command as a scheduled job. All my shell scripts run smoothly in a normal terminal in both Ubuntu 10.04 and 20.04 (3 different systems). I get all my results as expected. However, soon as I run my shell script with at, it does not run at all. Here are 3 cases to demonstrate that at works for simple operation and fails complex operation.
case 1
at works with simple script. Following is everything my shell script1.sh:
echo $(date) > ~/Desktop/time.txt
running at now + 1 minutes -f ~/script1.sh, it saves time into time.txt file. Great, it means that my at functions well.
case 2
at fails a simple script. Following is everything my shell script2.sh:
echo $(date) > ~/Desktop/time.txt
pycharm.sh
running at now + 1 minutes -f ~/script2.sh, it saves time into time.txt file, but the Pycharm was never opened. However, if I run sh script2.sh without using at, everything worked out perfectly, I get the time in time.txt file, and Pycharm opens normally. I think it simply means that at did not get all the variables in an existing terminal environment. Once I add export DISPLAY=:1 to script2.sh as
echo $(date) > ~/Desktop/time.txt
export DISPLAY=:1
pycharm.sh
then at now + 1 minutes -f ~/script2.sh would function correctly.
case 3
I need to run the following shell script with at as a scheduled job, script3.sh:
gjs -numberofsplits 50 -clusterplatform condor -condorscript condor.script main.mac > ./output0.txt 2>&1
condor_submit main.submit > ./output1.txt 2>&1
Please note that all these scripts runs normally in a terminal with sh script.sh, however, running the script with at now + 1 minutes -f ~/script3.sh, it throws an error saved in output0.txt file, it says
gjs: error while loading shared libraries: libG4global.so: cannot open shared object file: No such file or directory
I also tried to use the absolute path of gjs and condor_submit in script3.sh like
/home/albert/Products9/Gate/Gate-9.0-install/bin/gjs -numberofsplits 50 -clusterplatform condor -condorscript condor.script main.mac > ./output0.txt 2>&1
/usr/bin/condor_submit > ./output1.txt 2>&1
but it throws the same error.
solutions already tried
By far, I think the problem is well described. I think the cause of this problem is that at command does not have access to all the environment variables as in a normal terminal.
Here are the solutions I have tried posted here, hoping to get all environment variables exposed to at by adding
source $HOME/.profile
or
source $HOME/.bashrc
to the beginning of shell script. However, it does not solve or alleviate the problem at all.
asking for help
May I ask for help from you, the expert in this field, to solve this problem. As this problem may also trouble many other users. Thanks a lot.