Happy Hacks for Your Life

Hacks are a thing nowadays. At first, I didn’t understand what a hack was or how to use one. Then, my grandkids helped me wrap my head around the concept. “Life hacks are ways to get through life…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Cron Does Not Care About Environment

Recently I have faced a case while deploying a laravel application with scheduler on kubernetes. The jobs in scheduler needed to access the database but the logs were indicating that the scheduler jobs running with cronjob could not connect to the database. The following error log was showing up.

But the application was perfectly running without any problem to connect to the database. The debugging process unmasked that cron was not getting the environment variables i.e. the db credentials for the application.

So, we can create a cronjob resource named cron-test which will run the job every minute.

This Cronjob will execute the job every minute like this.

But in our case, we need application context. The application is heavy and we cannot guarantee a graceful execution of the job. So, we decided to create a deployment which will execute the jobs with help of cronjob.

To test this, we will first create a configmap named env which contains our environment variables.

Then, we will create a configmap for cronjobs and a deployment to run the cron.

Which is normally what you want, but when running inside a container docker will stop your container when your main process exits. to instruct cron to start in foreground mode, we have used cron -f command in the entrypoint. Notice the environment of the deployment comes from our previously configured configmap env.

You can see that in the pod, if I get the environment variables, I can get the variables NAME and AGE. But in /tmp/env.txt, where cron is writing the env variables, there the env variables NAME and AGE are not available. Cron is deliberately ignoring the environment here. So, we came to a solution that if we get the environment vairables in /etc/environment, which is a system-wide configuration file, then cron will use our desired environment. The command will be changed like this.

Now cron gets our desired env variables NAME and AGE. Therefore, they are in /tmp/env.txt file. It can be sometimes risky to expose the env variables in a system-wide configuration file. We can also choose specific environment variables or grep a set of environment variables for /etc/environment.

Add a comment

Related posts:

The little Fokkers go to war

It was the summer of 1971and a war was brewing in South Asia. Pakistan had been created as a Muslim majority country when British India gained independence in 1947, but it was a bifurcated state…

CEO Statement II

I reach out to you today with heartfelt sincerity and with the full weight of our dedicated team behind me. Firstly, I am excited to share with you some wonderful news. In the coming weeks, we will…

Send me your Cuckold Confessions!

This shout-out goes to my followers who are still here with me, and to my future followers as well. There’s no way that you’re going to be excluded. You all are the reason why I do what I do. To tell…