Wellcome to SuperUser.
First of all, maybe you're facing it the wrong way. If the goal is to restart the complete container every 15 minutes, maybe you should check the links bellow.
Question about restarting pods with multiple possible solutions
Pod lifecycle documentation
Kubernetes concepts - Explanation about how kubernetes works and the names people uses for things there.
Just for completity, and in case you actually want to run a periodic task, check specific Kubernetes docs, they state how to create cronjobs and debug them. I'd also suggest to read about Kubernetes limitations in documentation, as there may be something actively preventing to run your cronjobs
GCP Documentation about cronjobs
Kubernetes documentation about Cronjobs
I'm giving you a resume about how to create a cronjob task, but you should carefully read the above if you're not familiar with kubernetes and follow the links inside, they've environments ready to play and learn the basics of kubernetes.
It is more elaborated inside, but it basically gives you an example command and the way to deploy it. I've modified it to be once every 15 minutes and run a pkill. You've to create a yaml file with the following contents (check syntax for more complex cron behaviours):
# cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/15 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- pkill yourprocess
restartPolicy: OnFailure
and then deploy it using
kubectl apply -f [FILENAME]
Use the following command to check if your cronjob was deployed:
kubectl describe cronjob [CRON_JOB]
Post describing the above processes with more screenshots and examples
After this point this is a very generic answer. You can post comments and I'll try to help you out, but I currently have no access to an Alpine Linux installation, so I may not be helpful. This is the Alpine Linux documentation page addressing your issue
Tracing your error: The simple way
There's a very simple way to narrow down whats going out, although it is not very precise. There are two chances here: There's a problem with CRON itself, or you have a problem with your script (permissions, for instance). The process is simple:
- Create a task that creates an empty file inside a folder where you're ABSOLUTLY SURE everyone can read and write.
- If the new file appears when the task is suposed to run, it means that your cron is executing properly and that you have a problem with your script. Go to "Script problems" section. If not, you've a problem with cron. Go to Cron problems section
Tracing your error: Logs
The best way to solve problems in any system is looking at the logs. Software like cron normally stores logs on syslog (normally located at /var/log/syslog), unless configured otherwise. This post speaks about cron logging folders.
If you find out where your cron instance is logging and there's nothing there, it means that your cron service is not running at all. There's a Alpine Linux specific solution for this problem in Cron problems. If you can see errors, you should look at them and try to narrow down your problem. If not, you should make sure that your script is actually not running.
Cron problems
Ok, now you know cron is not working properly. There are various possible scenarios:
- Cron is not running at all
Maybe the cron service is not started. Check if the service is running. If it is not, try to start it manually. Check if it has started properly and then add it to startup process. The way to do so varies depending on the OS. In Alpine Linux, this should do the trick:
rc-service crond start && rc-update add crond
- The cron folder you're using is not being used by cron
You should check the main cron config file in order to verify that the folder you're putting scripts on is actually added.
Maybe the config line has some error with spaces on arguments or things like that, although in this case this seem not to be the scenario, as you used test commands on your folder.
Script problems
If your script runs but it is not producing the desired output, it is a good idea to perform the following steps:
- Separate your script into a file. Give it run permissions and ensure that the first line properly states the shell to use, in Alpine's case, this line should be #!/bin/sh
- Redirect the standard and error output to a file to analize what's going on.
General suggestions
- As stated in Script problems section, it is a good practise to separate your oneliners into script files and point their output to files, in order to be able to debug problems of all kinds.
- Take a close look at directory and file permissions (those directly accesed by your script and those that cron should read / user in order to execute your scripts), it is a common cause of failure, specially in containers.
EDIT: Maybe I understood your situation in the wrong way and you're in an scenario like this one
I know this may be a bit silly, but are you totally sure it is actually not executing the process? Maybe it is running and not producing the desired behaviour because of permissions? This is a common problem. I'm not familiar with Alpine Linux logging, but you can schedule a script that creates a new file (touch <path>) in a 777 folder. If the file doesnt appear, the script is not executing, if it does, it means that you dont have permission to kill that process or execute pkill at all. Also, check on the main cron configuration if the line for 15 minute cronjobs is commented out. – DGoiko – 2020-01-13T23:18:17.777
I assumed in my comment that you checked that you have the correct syntax on your command that that it actually works if you execute it manually. Also, for maintainability and testing reasons, I usually suggest to create a .sh script that does the job and then schedule it, even if it is just one line. It is allways easier to change and migrate things given that scenario, and you'll get less errors due to lack of attention during repetitive tasks specially if spaces are involved. Have you ever lost a closing " which is the last character while copying something out? It is really anoying – DGoiko – 2020-01-13T23:22:51.203
Some references I found:
https://stackoverflow.com/questions/37015624/how-to-run-a-cron-job-inside-a-docker-container https://forums.docker.com/t/how-to-run-a-cron-job-inside-a-container-alpine/7759
Check cron log: https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log
I tried to run the pkill command from the command prompt and it work perfectly fine. As you have said I tried doing the touch command it worked fine on my local docker image but it does not run on the Kube cluster. For Alpine linux they have mentioned that you should not create a .sh file, they have mentioned that you need to create a file without extensions to work. I have checked links that you have mentioned and tried it, but that does not work as well. On that docker image on Kube I cannot do sudo not su for any of the commands. – Nitesh Joshi – 2020-01-13T23:43:55.610
oh, I added a custom answer but it doesnt add much info. Please, try to find the log files and see if CRON is giving any errors. Also, create a file (without extension if you must) and redirect the output of the command to a file so see if something is going wrong. Redirect output to file: https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file. This should show nothing, as it is actually not running
– DGoiko – 2020-01-13T23:56:05.013Please, check https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/ and https://cloud.google.com/kubernetes-engine/docs/how-to/cronjobs?hl=es-419 aswell
– DGoiko – 2020-01-14T00:00:15.327check my answer again. Modified it with specific restarting info about kubernetes – DGoiko – 2020-01-14T00:24:41.023