I run screen command through cron, where the codes is in lorem.sh files.

This is cron codes: * * * * * cd /home/z; ./lorem.sh

Inside lorem.sh:

screen -S screenname -X stuff $'\033[B'
sleep 1 && screen -S screenname -X stuff 2
sleep 1 && screen -S screenname -X stuff "lorem ipsum dolor"
sleep 1 && screen -S screenname -X stuff $'\n'

Above lorem.sh codes: First line is "Arrow down", and forth line is "Enter"

When running lorem.sh through cron, only second & third line working.

Above lorem.sh works fine if run from terminal by typing ./lorem.sh but not working from cron

  • 2
    cron may be using /bin/sh as your shell instead of your usual shell (which is likely bash), and on some systems /bin/sh doesn't know about '$' in front of strings. If you edit lorem.sh to have #!/bin/bash as its first line, does it work? – Mark Plotnick Sep 2 '14 at 16:34
  • It working now. – apasajja Sep 4 '14 at 1:26

Most versions of cron run commands using /bin/sh by default, and if the commands run any shell scripts (that don't have a #! line to force use of a specific shell), /bin/sh will be used to run them, too.

On some systems, /bin/sh is dash, a shell that doesn't understand the ANSI-C quoting convention used by bash and other shells. So your $'\n' string is probably getting interpreted as the 3-character string $\n.

Most versions of cron will let you specify a shell to run your commands. You can have it use bash by editing your crontab to add a line


that comes before any lines that schedule jobs.

Alternatively, you can make lorem.sh always use bash by adding


as its first line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.