1

I have created a simple executable script to move a file for me, as the name of the file is the date on each day, the script creates the file name and then moves the file. I want to schedule it to run every night using cron.

My script works when I run it in bash. It also works if I specify exactly the path and filename in the cron.

The problem occurs when I ask cron to run my script. It seems to me cron cannot run the subprocess to create the filename based on the date. my script is as follow:

#!/bin/bash
mv /home/folder1/$(date "+%Y-%m-%d").txt /media/folder2/folder3

and this is how I set the job in crontab:

0 21 * * * /home/folder1/bash_text.txt

I have tried with $(which mv), with no luck. (tried both in the script and cron command)

0 21 * * * $(which mv) /home/folder1/$(date "+%Y-%m-%d").txt /media/folder2/folder3

This only worked if I substituted the name of the file $(date "+%Y-%m-%d").txt with the actual file name.

0 21 * * * $(which mv) /home/folder1/2020-04-28.txt /media/folder2/folder3

Appreciate it if someone can help me, please. I am new to Linux as you can tell, so I am learning as I go, appreciate if you consider this in your replies.

1 Answers1

0

I don't have enough reputation to comment ...

What is the error in each case?

See here how to find cron errors: Where are cron errors logged?

For the first setting

Does /home/folder1/bash_text.txt really have execute permission?

If so, it should work.

For the second setting

The link provided by Kusalananda should work.

In all cases

The default shell for executing commands in crontab is /bin/sh. Maybe this is not suitable...

You can modify it like this:

SHELL=/bin/bash

0 21 * * * command

For more information on using crontab:

$ man 5 crontab

Hope this helps.