-1

I can't understand, I have looked at several forums to help me, etc... But it still doesn't work! I would like to run a script thanks to Cron! To try, I'm currently trying to execute a script that sends "test" in commands (with echo test). No problem, this script works perfectly by hand if I call the file. But I tried to run my file with Crontab -e every minute and I've been waiting for several minutes already, but no results. And I can't really understand why!

Already, I was told to put #! /bin/bash at the beginning of my code in my script, but when I put it in I have an error and the code doesn't execute by hand. Whereas if I don't put anything in, the code runs smoothly. So I don't know if that's where the mistake came from, maybe.....

The final goal would be to make a script that runs every day, say to clear the cache with sync; echo 3 > /proc/sys/vm/drop_caches. What should look like in the Crontab : 00 20 * * * PATH if I'm not mistaken.

Do you have a solution to help me?

EDIT: -bash: /root/Discord/script/cache.sh: /bin/bash^M: bad interpreter: No such file or directory it's the error I got when I runned /root/Discord/script/cache.sh to execut my script. And that command works when I don't have #! /bin/bash. But that works when I used sh cache.sh in the directory, even with #! /bin/bash !

  • "Sends "test" in commands" what does this mean? Can you share your script? – JNevill Feb 19 '19 at 20:26
  • Actually I just did `echo test` ! But it's just to try, when it will works I will remplace this by `sync; echo 3 > /proc/sys/vm/drop_caches` ! – Sami Lafrance Feb 19 '19 at 20:29
  • 1
    "I have an error" is really vague. Can you copy-paste the command you run and the complete error message? – that other guy Feb 19 '19 at 20:34
  • I edited my post ! – Sami Lafrance Feb 19 '19 at 20:39
  • If you were to run `echo test` by cron, where would you anticipate it will echo that? `echo 'test' > /tmp/test.txt` in cron (or via script called by cron) and then check that location. – JNevill Feb 19 '19 at 20:49
  • You shouldn't be editing Linux files on Windows - wrong line endings. – tink Feb 19 '19 at 20:55
  • Where did I got a wrong line endings ? But yes, thanks for the advice ! – Sami Lafrance Feb 19 '19 at 20:59
  • And actually that works ! @JNevill , so I got the anwser in my test.txt even when I execute command from crontab ! But it's only in my .txt so, how can I do a command like `sync; echo 3 > /proc/sys/vm/drop_caches` ? – Sami Lafrance Feb 19 '19 at 21:02
  • Stick that in a script and call the script in cron. I can't see a reason it won't work. It will echo the number `3` replacing the contents of /proc/sys/vm/drop_caches with `3` (hopefully what you intend). This is assuming that the user's crontab you are editting has security to write to `/proc/sys/vm/drop_caches`. – JNevill Feb 19 '19 at 21:57

1 Answers1

1

Crontabs don't print the output on your opened terminal. You need to either create a file and then append the output there to view or test if it works. You can refer this answer https://stackoverflow.com/a/28856563/7181668

But if you want to run a shell script file through cron then you need to make sure you have given executable permission to that file and then you can use the below command in crontab -e

 * * * * * /bin/sh /home/myUser/scripts/test.sh
reflexgravity
  • 910
  • 10
  • 17
  • So for the moment I told Crontab to put me echo test in an .txt file every minute, but I only have one test in this file, while it's already been several minutes, the echo is displayed below the other and therefore my Crontab doesn't work continuously or simply the echo will just modify the first so rewrite each time test "over the other"? Just to know if I have to fix it first or if it's normal ? – Sami Lafrance Feb 19 '19 at 21:08
  • But thanks for your answer, I understood ! And my file is executable I think... I used `chmod +x test.sh` – Sami Lafrance Feb 19 '19 at 21:09
  • @SamiLafrance if you're echoing to a file then it'll rewrite it's contents. – reflexgravity Feb 19 '19 at 21:48